简体   繁体   中英

UIPanGestureRecognizer conflicts with UIPinchGestureRecognizer's superview

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?

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM