Search code examples
iosobjective-cuipinchgesturerecognizer

UIPinchGestureRecognizer Disable Pinch Out


I have added a pinch gesture recognizer to a scroll view, using it to close a modal view controller. I did it like so:

UIPinchGestureRecognizer *closePinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(closeGallery)];
[galleryScrollView addGestureRecognizer:closePinch];

Although it is added to a scrollView, I do not actually use it to zoom only to close the view. Therefore, I have no need for the pinch-out gesture as it indicates zooming in.

Is there a way to easily disable the pinch-out portion of the gesture recognizer and leave the pinch-in untouched?

Based on Crazyrems' answer, the following delegate method did exactly what I needed:

- (BOOL)gestureRecognizerShouldBegin:(UIPinchGestureRecognizer *)gestureRecognizer
{
    // Negative velocity indicates pinch out
    if (gestureRecognizer.velocity < 0) {
        return YES; // <- Register touch event
    } else {
        return NO; // <- Do not register touch event
    }
}

Solution

  • You should implement -gestureRecognizerShouldBegin: in your UIGestureRecognizerDelegate

    There's a velocity property in the recognizer passed in parameter, so you can check if it's a pinch in or out, and return YES or NO in consequence.