Updated for Swift 3.0
This is an old question that I stumbled across while solving an almost identical problem of adding a double tap recognizer to reset a slider.
The original answer points us in the right direction by adding an action to respond to UIControlEvents.touchDownRepeat
.
This can be done in swift by adding the line in your view controller (for example in the viewDidLoad()
method:
override func viewDidLoad()
{
super.viewDidLoad()
self.slider.addTarget(self, action: #selector(sliderTouchDownRepeat(_ :)), for: .touchDownRepeat)
}
And you can respond accordingly in the selector:
@objc func sliderTouchDownRepeat(_ slider: UISlider)
{
//Respond to double tap here
}
This is all well and good, although there are some additional pieces of code that are necessary if you plan on changing the slider value when the double tap is recognized.
First, also add an action for the UIControlEvents.valueChanged
override func viewDidLoad()
{
super.viewDidLoad()
self.slider.addTarget(self, action: #selector(sliderTouchDownRepeat(_ :)), for: .touchDownRepeat)
self.slider.addTarget(self, action: #selector(sliderDidChange(_:)), for: .valueChanged)
}
Cancel the current touch down event that the we are responding to so that the slider thumb is not immediately returned to the double tap location slider.cancelTracking(with: nil)
and call the action for the UIControlEvents.valueChanged
@objc func sliderTouchDownRepeat(_ slider: UISlider)
{
slider.cancelTracking(with: nil)
// Perform your own value change operation
// self.reset()
self.sliderDidChange(slider)
}
@objc func sliderDidChange(_ slider: UISlider)
{
//Update any other UI here
}
Happy coding