美文网首页
扩大UISlider滑块的触摸区域

扩大UISlider滑块的触摸区域

作者: 1bcaa968d78c | 来源:发表于2022-05-28 15:19 被阅读0次

    #import "MGSlider.h"

    #define thumbBound_x 20

    #define thumbBound_y 20

    @interface MGSlider : UISlider

    @property (nonatomic, assign) CGRect  lastBounds;

    @end

    @implementation MGSlider

    //扩大滑块触摸区域

    - (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value {

        rect.origin.x= rect.origin.x;

        rect.size.width= rect.size.width;

        CGRectresult = [super thumbRectForBounds:bounds trackRect:rect value:value];

        _lastBounds= result;

        return result;

    }

    - (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event{

        UIView*result = [super hitTest:point withEvent:event];

        if(point.x<0|| point.x>self.bounds.size.width){

          return result;

        }

        if((point.y>= -thumbBound_y) && (point.y<_lastBounds.size.height+thumbBound_y)){

                floatvalue =0.0;

                value = point.x-self.bounds.origin.x;

                value = value/self.bounds.size.width;

                value = value <0?0: value;

                value = value >1?1: value;

                value = value * (self.maximumValue-self.minimumValue) +self.minimumValue;

                [self setValue:value animated:YES];

        }

        return result;

    }

    - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event{

        BOOL result = [super pointInside:point withEvent:event];

        if(!result && point.y> -10) {

            if((point.x>=_lastBounds.origin.x-thumbBound_x) && (point.x<= (_lastBounds.origin.x+_lastBounds.size.width+thumbBound_x)) &&(point.y< (_lastBounds.size.height+thumbBound_y))) {

                result =YES;

            }

        }

          return result;

    }

    @end

    相关文章

      网友评论

          本文标题:扩大UISlider滑块的触摸区域

          本文链接:https://www.haomeiwen.com/subject/meviprtx.html