Custom UIGestureRecognizer for Two-Finger Gestures

I have a custom UIGestureRecognizer for two-finger gestures that works great, except that it is very picky about how the fingers should touch an iOS device at the same time touchesBeganfor a two-touch call. touchesBeganoften called with just one touch, although I'm trying to use two fingers.

Is there a way to recognize how many Touches are more forgiving regarding how you should place your fingers on the touch screen at the same time?

I noticed that two finger keys are recognized even when you place one finger first and then the other much later, still holding the first finger down.

Here is the code for my function touchesBegan:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {

      if touches.count != 2 {
         state = .failed
         return
      }

      // Capture the first touch and store some information about it.
      if trackedTouch == nil {
         trackedTouch = touches.min { $0.location(in: self.view?.window).x < $1.location(in: self.view?.window).x }
         strokePhase = .topSwipeStarted
         topSwipeStartPoint = (trackedTouch?.location(in: view?.window))!
         // Ignore the other touch that had a larger x-value
         for touch in touches {
            if touch != trackedTouch {
               ignore(touch, for: event)
            }
         }
      }
   }
+6
5

touchesBegan:withEvent: touchesEnded:withEvent: touchesMoved:withEvent:, , .failed, .ended

, touchesMoved:withEvent: . touchesEnded:withEvent:, , , , , , , .

+1

touchesBegan, , : .

, , (, , ), , , .

public class TwoFingerGestureRecognizer: UIGestureRecognizer {
    private var trackedTouches: Set<UITouch> = []

    public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
        for touch in touches {
            if self.trackedTouches.count < 2 {
                self.trackedTouches.insert(touch)
            }
            else {
                self.ignore(touch, for: event)
            }
        }

        if self.trackedTouches.count == 2 {
            // put your current logic here
        }
    }

    public override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
    }

    public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
        self.trackedTouches.subtract(touches)
    }

    public override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) {
        self.trackedTouches.subtract(touches)
    }

    public override func reset() {
        super.reset()

        self.trackedTouches = []
    }
}
+1

. touchBegan/Moved/Ended/Canceled " " ( Apple docs), , , . , multi-touch, , .

, , 2. , , , , , re , ( , touchhesBegan).

+1

, touchphonesMoved: withEvent , , , github.

.

0

Is touchsMoved an option so that you can achieve the result of desire? You can also execute a counter before setting the state to a failure state. Remember to set isMultipleTouchEnabled = true

  override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    if touches.count != 2 {
        print("we do NOT have two touches")
        if counter > 100   { // 100 "more fogiven"
            state = .failed
        }
        counter += 1
        return
    } else {
        print("we have to two touches")
    }
-1
source

All Articles