#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
网友评论