iosswiftuibuttonuipangesturerecognizeruilongpressgesturerecogni

Recognize both UILongPressGestureRecognizer and UIPanGestureRecognizer on same UIButton


I want to make a UIButton that when you long press it, it will start recording video and if you pan your finger vertically up (while still long pressing), the video will zoom in.

To my button I added a UILongPressGestureRecognizer and a UIPanGestureRecognizer that does just that. Individually, they work. However, they do not work together.

How can I make my button record when long pressing but also allow me to pan my finger and have that recognized as well? This is how I added my recognizers:

let long = UILongPressGestureRecognizer(target: self, action: #selector(record(gesture:)))
button.addGestureRecognizer(long)

let pan = UIPanGestureRecognizer(target: self, action: #selector(zoom(pan:)))
button.addGestureRecognizer(pan)

Solution

  • You need to confirm the delegate of those two gestures. for ex:

    let long = UILongPressGestureRecognizer(target: self, action: #selector(record(gesture:))) 
    long.delegate = self 
    button.addGestureRecognizer(long)
    
    let pan = UIPanGestureRecognizer(target: self, action: #selector(zoom(pan:))) 
    pan.delegate = self
    button.addGestureRecognizer(pan)
    

    and there is a delegate method to recognize multiple gestures simultaneously.

    gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)

    define that in your class and return true.

    you will get what you want.