0
votes

I'm adding both a UIPanGestureRecognizer and UIPinchGestureRecognizer to the same view. This typically would not cause any issues, but the requirement for 3 fingers with my UIPanGestureRecognizer is causing problems:

UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinched:)];
[self.view addGestureRecognizer:pinchGesture];

UIPanGestureRecognizer panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panning:)];
panGesture.minimumNumberOfTouches = 3;
panGesture.maximumNumberOfTouches = 3;
[self.view addGestureRecognizer:panGesture];

Occasionally, the pinch gesture will get called when the pan should have been. It works ~50% of the time, but what is a better way to implement these two gestures on the same view so the accuracy is better?

Edit: I only want one gesture to happen at a time.

2

2 Answers

0
votes

Set the delegates for the gestures to self and set your ViewController to implement UIGestureRecognizerDelegate

And add this method:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
0
votes

other post has a typo I think, but use this

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return NO;
}

and add this so other gestures will fail before the gesture you want to start

requireGestureRecognizerToFail