Custom UISlider - Increase “hot spot” size

前端 未结 6 754
时光说笑
时光说笑 2020-12-08 08:21

I have a custom UISlider that is relatively tough for big fingered people to grab hold of and slide due to the size of the \"thumb image\". Is there any way to increase the

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 08:55

    This solution works with 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))
        }
    }
    

    Credits go to this post: http://www.mpatric.com/2009-04-15-more-responsive-sliders-on-the-iphone

提交回复
热议问题