扩大UISlider滑块的触摸区域

#define thumbBound_x 10

#define thumbBound_y 20

@interface MGSlider:UISlider 

@property (nonatomic, assign) CGRect  lastBounds;

@end


@implementation MGSlider


//MARK:扩大滑块的触摸区域

- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rectvalue:(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)pointwithEvent:(UIEvent*)event{

    UIView*result = [super hitTest:pointwithEvent:event];

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

      returnresult;

    }

    if((point.y>= -thumbBound_y) && (point.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)pointwithEvent:(UIEvent*)event{

    BOOL result = [super pointInside:pointwithEvent: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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容