Crashalot Crashalot - 1 year ago 121
Swift Question

iOS: implement one-finger pan and two-finger pan gestures?

The goal is to implement two pan gestures against the same SCNScene: one with one finger and the other with two fingers.

This code below isn't working. The one-finger pan function never gets invoked even though the gesture is assigned a distinct selector. One-finger pans and two-finger pans both invoke


From reading other questions it seemed like
might be the answer, but these pans are not happening simultaneously. It should be either a one-finger pan or a two-finger pan, never both at once.

Is it possible to have two pan gestures as described above? If so, what's the right way to do this?

// Handle one-finger pans
let panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(sceneViewPannedOneFinger))

// Handle two-finger pans
let twoFingerPanRecognizer = UIPanGestureRecognizer(target: self, action: #selector(sceneViewPannedTwoFingers))

func sceneViewPannedTwoFingers(sender: UIPanGestureRecognizer) {
print("two finger pan!!!")

func sceneViewPannedOneFinger(sender: UIPanGestureRecognizer) {
print("one finger pan!!!")

Answer Source

You need to add UIGestureRecognizerDelegate to your view controller and set the gesture recognizers delegate to self inside your view controller. Add the shouldRecognizeSimultaneouslyWithGestureRecognizer method returning true for them. Make sure you set minimum and maximum number of touches also for them.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download