自定义UIGestureRecognizer并不总是调用其操作方法

时间:2018-07-07 11:24:30

标签: ios swift uigesturerecognizer

我有一个自定义UIGestureRecognizer,它可以正确识别预期的手势(两个手指的Z手势),并在state = .recognized中设置touchesEnded。问题在于,即使识别出手势,它有时也会调用该动作方法,而有时却无法。据我所知,这是不确定的。

有人知道为什么吗?

代码如下:

import UIKit
import UIKit.UIGestureRecognizerSubclass

class ZGestureRecognizer: UIGestureRecognizer {

    private var topSwipeStartPoint = CGPoint.zero
    private var diagonalSwipeStartPoint = CGPoint.zero
    private var bottomSwipeStartPoint = CGPoint.zero
    private let minDeltaX: CGFloat = 20
    private let minDeltaY: CGFloat = 20
    private var strokePhase = StrokePhase.notStarted
    var trackedTouch: UITouch?


    enum StrokePhase {
        case notStarted
        case topSwipe
        case diagonalSwipe
        case bottomSwipe
    }


    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
        if !(touches.count == 1 || touches.count == 2) {
            state = .failed
            return
        }

        if trackedTouch == nil {
            trackedTouch = touches.min { $0.location(in: self.view?.window).x < $1.location(in: self.view?.window).x }
            strokePhase = .topSwipe
            topSwipeStartPoint = trackedTouch!.locationInWindow!
        }
    }


    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) { 
        guard let trackedTouch = trackedTouch,
            trackedTouch.phase != .cancelled,
            trackedTouch.phase != .ended
        else {
           self.state = .failed
           return
        }

        let newPoint = trackedTouch.locationInWindow!
        let previousPoint = trackedTouch.previousLocationInWindow!

        switch strokePhase {
        case .topSwipe:
            if newPoint.x < previousPoint.x { // if we have started moving back to the left
                let deltaX = previousPoint.x - topSwipeStartPoint.x

                if deltaX > minDeltaX && touches.count == 2 {
                   diagonalSwipeStartPoint = previousPoint
                   strokePhase = .diagonalSwipe
                }
                else { // too short right swipe or not 2 touches
                   state = .failed
                   return
                }
             }

        case .diagonalSwipe:
            if newPoint.x > previousPoint.x { // if we have started moving back to the right
                let deltaX = diagonalSwipeStartPoint.x - previousPoint.x
                let deltaY = previousPoint.y - diagonalSwipeStartPoint.y

                if deltaX > minDeltaX && deltaY > minDeltaY && touches.count == 2 {
                   bottomSwipeStartPoint = previousPoint
                   strokePhase = .bottomSwipe
                }
                else { // too short right swipe
                   state = .failed
                   return
                }
             }

        case .bottomSwipe:
            if newPoint.x < previousPoint.x || touches.count != 2 {
               state = .failed
               return
            }

        default: break
        }
    }


   override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) {
      state = .failed
   }


   override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
      guard strokePhase == .bottomSwipe else {
         state = .failed
         return
      }

      let endPoint = trackedTouch!.locationInWindow!
      let bottomSwipeDeltaX = endPoint.x - bottomSwipeStartPoint.x

      if bottomSwipeDeltaX > minDeltaX {
         state = .recognized
      }
      else {
         state = .failed
      }
   }


   override func reset() {
      strokePhase = .notStarted
      trackedTouch = nil
   }

}

由于手势识别在执行适当的手势时总是在state = .recognized行中结束,因此我认为代码无关紧要,但是我很高兴会出错。

1 个答案:

答案 0 :(得分:0)

我已经模拟了您的代码。我无法获得正确的手势(您没有提到所需的手势)。因此,在我的情况下,从未执行过= .recognized。然后,我在touchesEnded中对state = .recognized进行了硬编码,以检查每次设置state = .recognized时是否都调用了该操作方法。我发现很好。

我发现所有覆盖的方法都缺少超级调用。您可以发出所有超级呼叫,然后再次检查。另外,将日志记录结束。我认为可能存在逻辑问题。因为,我不知道触发它所需的手势,所以无法调试代码。

相关问题