Micro
Micro

Reputation: 10891

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)

Upvotes: 0

Views: 327

Answers (2)

Jesse Seidman
Jesse Seidman

Reputation: 17

I know this wasn't exactly what the question was asking but you can actually bypass having to use gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:) and use UILongPressGestureRecognizer as a UIPanGestureRecognizer using the UIGestureRecognizer.State changes. Thats what i've done in the past, cleans things up and makes more logical sense than having two gesture recognizers

Upvotes: 0

Rahul Patel
Rahul Patel

Reputation: 534

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.

Upvotes: 4

Related Questions