disable gesture recognizer only for particular view

Ravindra Bagale picture Ravindra Bagale · Feb 8, 2013 · Viewed 17k times · Source

on one viewController i have one mainView, on that view i have one another view (which is known as sidePanel view having frame 0,0,86,420.)I have added tapGesture. Now i want to just enable gesture recognition only for mainView other than sidePanelView. see below image
enter image description here

i want to disable tapGesture for sidePanelView & enable for area other than it. How to do it. (one thing i want to say area other than sidePanelView is parentView of sidePanelView).

Answer

Rob picture Rob · Feb 9, 2013

You should accept Bharat's answer because that is correct. I only want to illustrate how you do it:

  1. Define your view controller as conforming to UIGestureRecognizerDelegate, e.g.:

    @interface ViewController () <UIGestureRecognizerDelegate>
    // the rest of your interface
    @end
    
  2. Make sure you set the delegate for the gesture:

    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleMainTap:)];
    gesture.delegate = self;
    [self.view addGestureRecognizer:gesture];
    
  3. Then have and then check to see if the touch takes place for the view in question:

    - (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    {
        if (CGRectContainsPoint(self.menuView.bounds, [touch locationInView:self.menuView]))
            return NO;
    
        return YES;
    }