uns*_*ero 17 objective-c uislider ios
我有一个定制的UISlider,对于大手指的人来说,由于"拇指图像"的大小而难以抓住并滑动.有没有办法在不改变图像大小的情况下增加可点击/可拖动区域的大小?
这是我创建自定义滑块的代码,如果有帮助的话:
[slider setMaximumTrackImage:[[UIImage imageNamed:@"max.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 20, 0, 20)]
forState:UIControlStateNormal];
[slider setMinimumTrackImage:[[UIImage imageNamed:@"min.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 20, 0, 20)]
forState:UIControlStateNormal];
[slider setThumbImage:[UIImage imageNamed:@"thumb.png"]
forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
uns*_*ero 26
我最终继承了UISlider并重写了这个方法:
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event {
CGRect bounds = self.bounds;
bounds = CGRectInset(bounds, -10, -15);
return CGRectContainsPoint(bounds, point);
}
Run Code Online (Sandbox Code Playgroud)
这使得可触摸区域在左侧和右侧延伸10个像素,在顶部和底部延伸15个像素.
此解决方案适用于iOS 8:
class ExtUISlider: UISlider {
var thumbTouchSize : CGSize = CGSizeMake(50, 50)
override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
let bounds = CGRectInset(self.bounds, -thumbTouchSize.width, -thumbTouchSize.height);
return CGRectContainsPoint(bounds, point);
}
override func beginTrackingWithTouch(touch: UITouch, withEvent event: UIEvent) -> Bool {
let thumbPercent = (value - minimumValue) / (maximumValue - minimumValue)
let thumbSize = thumbImageForState(UIControlState.Normal)!.size.height
let thumbPos = CGFloat(thumbSize) + (CGFloat(thumbPercent) * (CGFloat(bounds.size.width) - (2 * CGFloat(thumbSize))))
let touchPoint = touch.locationInView(self)
return (touchPoint.x >= (thumbPos - thumbTouchSize.width) &&
touchPoint.x <= (thumbPos + thumbTouchSize.width))
}
}
Run Code Online (Sandbox Code Playgroud)
积分转到这篇文章:http://www.mpatric.com/2009-04-15-more-responsive-sliders-on-the-iphone
斯威夫特3
UISlider增加触摸面积的子类
class CustomSlider: UISlider {
private var thumbTouchSize = CGSize(width: 40, height: 40)
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
let increasedBounds = bounds.insetBy(dx: -thumbTouchSize.width, dy: -thumbTouchSize.height)
let containsPoint = increasedBounds.contains(point)
return containsPoint
}
override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
let percentage = CGFloat((value - minimumValue) / (maximumValue - minimumValue))
let thumbSizeHeight = thumbRect(forBounds: bounds, trackRect:trackRect(forBounds: bounds), value:0).size.height
let thumbPosition = thumbSizeHeight + (percentage * (bounds.size.width - (2 * thumbSizeHeight)))
let touchLocation = touch.location(in: self)
return touchLocation.x <= (thumbPosition + thumbTouchSize.width) && touchLocation.x >= (thumbPosition - thumbTouchSize.width)
}
}
Run Code Online (Sandbox Code Playgroud)