Double touch on UIButton
If you are working with swift 5, @kennytm's solution won't work. So you can write an objective-c/swift function and add it as gesture with number of desired taps.
let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
tap.numberOfTapsRequired = 2
btn.addGestureRecognizer(tap)
and then
@objc func doubleTapped() {
// your desired behaviour.
}
Here button can be tapped any number of times and it will show as tapped. But the above function won't execute until button tapped for required number of times.
Add an target-action for the control event UIControlEventTouchDownRepeat, and do action only when the touch's tapCount
is 2.
Objective-C:
[button addTarget:self action:@selector(multipleTap:withEvent:)
forControlEvents:UIControlEventTouchDownRepeat];
...
-(IBAction)multipleTap:(id)sender withEvent:(UIEvent*)event {
UITouch* touch = [[event allTouches] anyObject];
if (touch.tapCount == 2) {
// do action.
}
}
As @Gavin commented, double-tap on a button is an unusual gesture. On the iPhone OS double-tap is mostly used for zoomable views to zoom into/out of a region of focus. It may be unintuitive for the users if you make the gesture to perform other actions.
Swift 3:
button.addTarget(self, action: #selector(multipleTap(_:event:)), for: UIControlEvents.touchDownRepeat)
And then:
func multipleTap(_ sender: UIButton, event: UIEvent) {
let touch: UITouch = event.allTouches!.first!
if (touch.tapCount == 2) {
// do action.
}
}