使用gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer

时间:2016-05-07 14:06:59

标签: ios swift uigesturerecognizer simultaneous

我试图在我的应用上同时检测上下滑动。我有一个SKScene,我在func didMoveToView: view中添加了手势识别器 我已设置委托:UIGestureRecognizerDelegate,并使以下func全部返回true。

gestureRecognizerShouldBegin: gestureRecognizer
gestureRecognizer: shouldRecognizeSimultaneouslyWithGestureRecognizer
gestureRecognizer: shouldReceiveTouch

当我用一根手指滑动时,我可以看到shouldRecognizeSimultaneouslyWithGestureRecognizer被触发并返回true。连接到滑动的Func也正在工作。

但是当我尝试同时向两个方向滑动时,不会触发相应的func,并且不会触发shouldRecognizeSimultaneouslyWithGestureRecognizer

当我单独执行滑动时滑动效果很好,但同时没有滑动执行。

我在这里做错了什么?

编辑:

class GameControlller: SKScene, SKPhysicsContactDelegate, UIGestureRecognizerDelegate {}

添加滑动:

 override func didMoveToView(view: SKView) {
    print("did move to view")
    let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(GameControlller.PaddleMoveUp(_:)))
    swipeDown.direction = .Down
    self.view!.addGestureRecognizer(swipeDown)

    let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(GameControlller.PaddleMoveDown(_:)))
    swipeUp.direction = .Up
    self.view!.addGestureRecognizer(swipeUp)
    swipeDown.delegate = self
    swipeUp.delegate = self
}

UIGestureRecognizer委托func:

func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}


 func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    print("Simultaneous gesture recognizer!")
    return true
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
    return true
}

1 个答案:

答案 0 :(得分:1)

我猜你忘了设置委托:

swipeUp.delegate = self
swipeDown.delegate = self