0

I am using a UIPinchGestureRecognizer, which uses 2 fingers by default. If a user decides to perform the multitask gesture, the pinch gestures action is also activated.

Is there a way to cancel the pinch gesture from occurring if more than four UITouch instances are detected?

Edit Removed sample code as it was the wrong approach.

ArtSabintsev
  • 5,170
  • 10
  • 41
  • 71

2 Answers2

1

With a multitask gesture, the numberOfTouches returned by the UIPinchGestureRecognizer is 2 instead of 4 or 5, because some touches are ignored.

You can subclass UIPinchGestureRecognizer and override ignoreTouch:forEvent to cancel the recognizer if the event has 4 or 5 touches:

- (void) ignoreTouch:(UITouch*)touch forEvent:(UIEvent*)event
{
    [super ignoreTouch:touch forEvent:event];

    // Cancel recognizer during a multitask gesture
    if ([[event allTouches] count] > 3)
    {
        self.state = UIGestureRecognizerStateCancelled;
    }
}
Donkey
  • 1,176
  • 11
  • 19
0

Since you're not subclassing the UIPinchGestureRecognizer, you shouldn't be using touchBegan:withEvent:. Instead you should be handling it in the method that is called when a pinch occurs.

- (void)handlePinch:(UIPinchGestureRecognizer *)pinchGestureRecognizer
{
    // if there are 2 fingers being used
    if ([pinchGestureRecognizer numberOfTouches] == 2) {
        // do stuff
    }
}
Craig Siemens
  • 12,942
  • 1
  • 34
  • 51
  • I ended up doing that a little while after posting this question. The problem is that iOS sometimes detects only 2 or 3 fingers immediately when you try doing the four-finger swipe, and the pinch goes off instead of the taskbar appearing. – ArtSabintsev Mar 24 '13 at 16:37