Tuesday, 10 July 2018

Custom UIGestureRecognizer does not always call its action method

I have a custom UIGestureRecognizer which recognizes the intended gesture correctly and sets state = .recognized in touchesEnded. The problem is that even though the gesture is recognized, it sometimes calls the action method and sometimes not. This happens undeterministically as far as I can tell.

Does anyone know why?

Here is the code:

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
   }

}

As gesture recognition always ends in the line state = .recognized when doing the appropriate gesture, I thought that the code was not relevant, but I'll be happy to be wrong.



from Custom UIGestureRecognizer does not always call its action method

No comments:

Post a Comment