Search code examples
iosswiftuislider

How to delay start touch event of a UISlider in iOS?


In my application, there is a UISlider which is only slidable after a long-press (let's say, after 0.1 seconds of the press). To prevent from the slider to interact immediately, I set mySlider.isUserInteractionEnabled = false as default. However, since .isUserInteractionEnabled will disable all the functions, including the long-press gesture, I use another transparent UIButton of the same size as the slider on the top of it to detect long-presses, and set mySlider.isUserInteractionEnabled = true in the callback action. This works fine, except that the UIButton must be released first and then the user tap the slider again to slide it instead of drag the slider directly within the same touch. This make the slider very inconvenient to use. Therefore, I would like to know if either:

  1. There is a better way other than mine to make UISlider start sliding only after certain delay? I'm quite surprised that I couldn't find any solution on this. I thought this would be a common need for a slider to be locked at a certain point.

or

  1. Following my solution, is there any way that I could start dragging the slider thumb within the same touch?

Thanks a lot for any kind of answer.

Added: My solution looks like the following: in the TableViewCell, there is a label, a slider, and an invisible button with the same size as the slider (The grey background area). I set slider attribute isUserInteractionEnabled == false, and then add gesture recogniser to manipulate the slider (such as single tap, double tap for my custom functions, and long-press gesture which enable the slider). After the button is long-pressed, the isUserInteractionEnabled of the slider will set to true until the thumb goes to the new value.

Basically I only want to disable the slider to respond too quickly for short taps, the other features of the slider would remain the same. I suppose there may be better ways to achieve this, the so far using an transparent button is the only way I could think of.

enter image description here


Solution

  • You can disable user interaction on slider and then add a gesture recogniser over it (Not exactly sure about user interaction though). Maybe do something like this

    let longPress = UILongPressGestureRecognizer(target: self.slider, action: Selector("longPressAndSlide:"))
    longPress.minimumPressDuration = 0
    self.addGestureRecognizer(longPress)
    

    In longPressAndSlide you can calculate the direction and delta of movement and set the value of slider accordingly.

    This has been answer multiple times actually. just a better search would help you more.

    Update Check answers under - How to enable "tap and slide" in a UISlider?

    Latest Update As the comments suggest, we can add a button on top of the slider and add longPress gesture over it. The handling can be done in the handler Action for the gesture.