1

I'm working with Swift 3, Xcode and SpriteKit.

I have two gestures: UIPanGestureRecognizer and UIPinchGestureRecognizer

let pinchGesture = UIPinchGestureRecognizer()
let panGesture = UIPanGestureRecognizer()

override func didMove(to view: SKView)
{        
    let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(GameScene.pinchFunction))
    view.addGestureRecognizer(pinchGesture)

    let panGesture = UIPanGestureRecognizer(target: self, action: #selector(GameScene.panFunction))
    view.addGestureRecognizer(panGesture)
}

I want to be able to pinch the screen, and while pinching to move my two fingers, so I want the two gestures to work simultaneously. But I can't manage to do it. Here's what I tried :

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

What is the correct way to do it ?

Drakalex
  • 1,488
  • 3
  • 19
  • 39
  • do you want to only pan when pinching, or do you want to do pan regardless of pinching – Knight0fDragon Nov 22 '16 at 21:31
  • I want to pan regardless of pinching and vice versa – Drakalex Nov 22 '16 at 21:33
  • then you need a seperate function that both gestures will call – Knight0fDragon Nov 22 '16 at 21:34
  • I'm not sure to understand what you mean – Drakalex Nov 22 '16 at 21:35
  • You can't have 2 gestures going, it does not make sense. So you make a function that handles your panning, then you have the pan function call this, as well as the pinch function after the punch function does its pinch work – Knight0fDragon Nov 22 '16 at 21:42
  • I think I get it, but I don't see how I could do that. I'm not in a hurry so could you make a really quick sample of code to show me what you mean ? And what is `shouldRecognizeSimultaneouslyWithGestureRecognizer` used for ? – Drakalex Nov 22 '16 at 21:49
  • That means 1 recognizer is used for 2 things, like pan and swipe are both recognized by 1 recognizer – Knight0fDragon Nov 22 '16 at 21:59
  • Your method shouldRecognizeSimultaneouslyWithGestureRecognizer it is not getting called because the gestureRecognizer parameter should have an underscore preceding it. Try `func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {`. Tip: Next time let Xcode autocomplete it for you. – Leo Dabus Nov 23 '16 at 02:31
  • I am voting to close this question as a typo – Leo Dabus Nov 23 '16 at 02:36
  • @Knight0fDragon So I create a `func gestureFunction(gesture: UIGestureRecognizer)` and then I check the number and the type of each gesture ? – Drakalex Nov 23 '16 at 17:41
  • So I resumed my tests and searching, but I'm still stuck. I don't get how to do it. Could someone explain it more clearly ? – Drakalex Nov 24 '16 at 17:02

0 Answers0