20

I'm trying to enable simultaneous gesture recognition but only for the UIPinchGestureRecognizer and UIRotationGestureRecognizer gestures. I don't want it to work for any other gestures. If I set the following property to true it allows all gestures to be recognized simultaneously, how can I limit it to just rotating and scaling?

func gestureRecognizer(UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}
budiDino
  • 13,044
  • 8
  • 95
  • 91
SpaceShroomies
  • 1,635
  • 2
  • 17
  • 23

2 Answers2

56

Make sure your class implements UIGestureRecognizerDelegate

class YourViewController: UIViewController, UIGestureRecognizerDelegate ...

Set the gesture's delegate to self

yourGesture.delegate = self

Add delegate function to your class

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    if (gestureRecognizer is UIPanGestureRecognizer || gestureRecognizer is UIRotationGestureRecognizer) {
        return true
    } else {
        return false
    }
}
Michael
  • 9,639
  • 3
  • 64
  • 69
Bannings
  • 10,376
  • 7
  • 44
  • 54
  • 12
    Don't forget to make yourself a UIGestureRecognizerDelegate – Pbk Apr 21 '16 at 05:23
  • 10
    and don't forget to set yourGesture.delegate = self – budiDino Jul 26 '16 at 23:04
  • 1
    @budidino I am adding the gestures on a SubClass of UIImageView. I have enabled userInteraction, set the delegate of both the gestures and still it wont call the delegate method. – Skywalker May 25 '18 at 06:44
  • @Skywalker I'm in the same boat (sub class of a `UIView` inside a `UIScrollView`): Added everything but the func is never called. Did you manage to fix it? – Neph Mar 22 '21 at 14:42
-1

any 2 cents for swift 5.1

// suppose You need to prefer pinch to pan:

//UIGestureRecognizerDelegate
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith shouldRecognizeSimultaneouslyWithGestureRecognizer: UIGestureRecognizer) -> Bool {

        if gestureRecognizer is UIPinchGestureRecognizer {
            return true
        }
        return false
    }
ingconti
  • 10,876
  • 3
  • 61
  • 48