iOS:识别捏手势而不是两个手指平底锅?

时间:2016-08-22 07:13:17

标签: ios swift uigesturerecognizer uipinchgesturerecognizer

目标是只在没有进行双指平移的情况下才能识别捏合手势。

此代码允许pinchGesture和双指panGestures,但它们同时发生。这是基于this answer以使用gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer: UIGestureRecognizer)函数。

    // Handle two-finger pans
    let twoFingerPanRecognizer = UIPanGestureRecognizer(target: self, action: #selector(sceneViewPannedTwoFingers))
    twoFingerPanRecognizer.minimumNumberOfTouches = 2
    twoFingerPanRecognizer.maximumNumberOfTouches = 2
    twoFingerPanRecognizer.delegate = self
    sceneView.addGestureRecognizer(twoFingerPanRecognizer)

    // Handle pinches
    let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(sceneViewPinched))
    pinchGesture.delegate = self
    sceneView.addGestureRecognizer(pinchGesture)

    func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer: UIGestureRecognizer) -> Bool {
       if gestureRecognizer is UIPinchGestureRecognizer {
          return false
       } else {
          return true
       }
     }

此代码允许双指平移,但是根本不起作用。

    // Handle two-finger pans
    let twoFingerPanRecognizer = UIPanGestureRecognizer(target: self, action: #selector(sceneViewPannedTwoFingers))
    twoFingerPanRecognizer.minimumNumberOfTouches = 2
    twoFingerPanRecognizer.maximumNumberOfTouches = 2
    twoFingerPanRecognizer.delegate = self
    sceneView.addGestureRecognizer(twoFingerPanRecognizer)

    // Handle taps
    let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(sceneViewTapped))
    sceneView.addGestureRecognizer(tapRecognizer)

    // Handle pinches
    let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(sceneViewPinched))
    pinchGesture.delegate = self
    pinchGesture.requireGestureRecognizerToFail(twoFingerPanRecognizer)
    sceneView.addGestureRecognizer(pinchGesture)

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

1 个答案:

答案 0 :(得分:3)

你有没有试过这个

{{1}}