Search code examples
iosiphoneobjective-ctouchgesture

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?


Solution

  • 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.