4
votes

I have a UIScrollView instance with a subview that has its own UIPanGestureRecognizer which is used to move the subview inside the scrollview. I would like the pinch-to-zoom feature of the scrollview to have the top priority over the pan gesture. However, this is not the case: when starting to pinch with one finger over my subview, it will pan instead.

What I tried so far:

  • gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:: it will pan AND pinch
  • [panGestureRecognizer requireGestureRecognizerToFail:scrollView.pinchGestureRecognizer];: panning just won't work anymore (callback is triggered only for UIGestureRecognizerStateEnded state). And panGestureRecognizer.cancelsTouchesInView = NO won't help.

My understanding is that when starting to pinch from the subview, both the scroll view and the subview only receive one touch each, that's why in the second case the pinch gesture recognizer doesn't even fail because it doesn't even begin to handle the event.

So, any idea on how to achieve this?

1

1 Answers

4
votes

One way to do it is to allow simultaneous interaction with the scrollView.pinchGestureRecognizer, and then cancel the panGestureRecognizer if the scrollView.pinchGestureRecognizer is actually zooming. The only way I know how to cancel them is to disable/enable.

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

    if (gestureRecognizer == panGestureRecognizer){
        if (otherGestureRecognizer == scrollView.panGestureRecognizer) {
            return 0;
        }
        else if(otherGestureRecognizer == scrollView.pinchGestureRecognizer){
            if (scrollView.pinchGestureRecognizer.scale != 1) {
                gestureRecognizer.enabled = 0;
                gestureRecognizer.enabled = 1;
            }
        }
    }
    return 1;
}