Search code examples
ioscocoa-touchuiresponder

Disable all UIPanGestureRecognizers after `touchesBegan:withEvent:`


Is there a way to disable all of the UIPanGestureRecognizers that a touch effects? I am hoping to be able to isolate all touch events to one of my subviews and have every superview ignore all the touch events, but I can only determine this after touchesBegan:withEvent:.

Is it possible to stop my superview's UIPanGestureRecognizers from interacting with a touch after it has triggered touchesBegan:withEvent:?


Solution

  • To disable and re-enable panning in all superviews, you should do something like this:

    - (void)recursivelyEnable:(BOOL)enable panGesturesInSuperview:(UIView *)superview
    {
        for(UIPanGestureRecognizer *recognizer in superview.gestureRecognizers)
        {
            if([superview isKindOfClass:[UIScrollView class]])[(UIScrollView *)superview setScrollEnabled:enable];
            else [recognizer setEnabled:enable];
        }
        if(superview.superview)[self recursivelyEnable:enable panGesturesInSuperview:superview.superview];
    }
    

    and use it like so:

    //Disable panning
    [self recursivelyEnable:NO panGesturesInSuperview:self.superview];
    
    //Enable panning
    [self recursivelyEnable:YES panGesturesInSuperview:self.superview];
    

    For some reason, you can't mess around with the UIGestureRecognizers of a UIScrollView or any of it's subclasses; that is why I've included the check and alternative dis/enabling of panning.