Search code examples
swiftuislider

UISlider: Touch ignore track


Is their a way to ignore touches of the track and only show the thumb-slider. Because of a particular feature the UISlider was used and it's track was hidden depending if the user was touching it or not. The issue is that the slider is over another 3D view and even though when hidden only the thumb-slider shows, the hidden slider captures the event. instead of the uiview beneath it. Is their a way to ignore the touching of the slider.

The only work around I can think of now is to listen for when the user touches the slider and when the user releases place an image of the ThumbImage in the exact position the actual Thumb for the slider was; however, I was hoping their was an easier way around it.

I.E. easier to think of it as a slider over a Apple MapView whose slider interfering with user touch


Solution

  • There is not a completely kosher way to do this with a UISlider.

    You can do it by subclassing UISlider and overriding pointInside(_:withEvent:) to only return true if the point is in the thumb area, and you can get the thumb area using thumbRectForBounds(_:trackRect:value:), but the documentation of that method says “You should not call this method directly.”

    Nevertheless, this seems to work:

    class MySlider: UISlider {
    
        override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
            let thumbRect = thumbRectForBounds(bounds, trackRect: trackRectForBounds(bounds), value: value)
            return thumbRect.contains(point)
        }
    
    }