简体   繁体   中英

Prevent the superview recognizing the pan gesture (in iOS 7)

I have implemented a Side Bar NavigationController , which consists of a front and a rear ViewController . This Side Bar NavigationController does make use of pan and swipe gesture recognizers to let the user toggle between the front and the rear view. It works fine. But using a TableView in the front ViewController causes an annoying behaviour. Everytime I scroll in the table view, the pan gesture recognizer of my Side Bar NavigationControllers recognizes the horizontal movement of my finger and moves the front view controller accordingly.

Now, here is my question: Is it possible to prevent the superview (Side Bar NavigationController's view) recognizing the pan gesture as I scroll in the table view? It somehow works fine with the back swipe gesture of the navigation controller, since as swiping back (from left to right) my Side Bar NavigationController does not recognize the pan gesture. It also works perfectly with a UISlider in the front view. So, I can move the thumb of the slider from left to the right and my Side Bar NavigationController does not recegnize the pan gesture at all. So, somehow the slider prevents forwarding the touch events to its superviews. How can I achieve the same with the table view?

Try some of the following:

  • Set the gesture delegate and prevent the 2 gestures to be recognised simultaneously
  • Implement UIScreenEdgePanGestureRecognizer
  • In gesture delegate check the location of the gestures when the state is UIGestureRecognizerStateBegan and call both gesture.enabled = NO; gesture.enabled = YES; gesture.enabled = NO; gesture.enabled = YES; to break gesture recognising in cases where the location is in table view.

Unfortunately each of the approaches might have side affects though.

I had the same problem in one of my earlier projects.

I ended up overriding shouldBeRequiredToFailByGestureRecognizer in the view controller (UIGestureRecognizerDelegate function) to prevent an other view from stealing my gestures

//Prevents the map from stealing gestures from headerview
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    if(gestureRecognizer is your gesture)
        return YES;

    return NO;
}

Remember to set the delegate of your gesture recognizers!

I modified the gesture recognition implemented in my SideBarNavigationController and I am now able to achieve the desired behaviour.

So, I only had to change the implementation of the delagate method gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:. Before, it simply returned YES, so that the SideBarNavigationController could detect swipe and pan gesture simultaneously. Now the implementation is changed to the following:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    BOOL shouldRecognize = NO;

    if (  (  (gestureRecognizer == _rightSwipeGestureRecognizer)
           ||(gestureRecognizer == _leftSwipeGestureRecognizer)
           ||(gestureRecognizer == _panGestureRecognizer))
        &&(  (otherGestureRecognizer == _rightSwipeGestureRecognizer)
           ||(otherGestureRecognizer == _leftSwipeGestureRecognizer)
           ||(otherGestureRecognizer == _panGestureRecognizer)))
    {
        shouldRecognize = YES;
    }

    return shouldRecognize;
}

The method now checks if the two given gesture recognizers are equal to those created in my SideBarNavigationController. If so, the method returns YES. Otherwise, if one of the gesture recognizers is coming from another view, like the table view, the methoed returns NO, so that the SideBarNavigationController stopps detecting the swipe and pan gestures.

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