Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable gesture recognizer only for a particular view

On one view controller, I have one mainView. On that view I have another view, sidePanel, which has the frame 0,0,86,420. I have added a tap gesture recognizer. Now I want to just enable gesture recognition only for mainView and not for sidePanelView. See below image:

View on left going over bottom view

I want to disable tapGesture for sidePanelView and enable for all areas other than it. How can I do that? (One other thing I want to say, area other than sidePanelView is parentView of sidePanelView).

like image 670
Ravindra Bagale Avatar asked Feb 08 '13 12:02

Ravindra Bagale


6 Answers

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;
    }
    
like image 142
Rob Avatar answered Oct 13 '22 01:10

Rob


You could use the gestureRecognizer:shouldReceiveTouch: method in your UIGestureRecognizerDelegate to see where the touch occurred and decide whether or not you want to respond to the gesture. Return NO if the touch is too close to the edge of your View(where you want ti disabled), otherwise return YES. Or simply check the touch.view to see if the touch occurred on your UIImageView.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
   shouldReceiveTouch:(UITouch *)touch;
like image 42
Bharat Gulati Avatar answered Oct 13 '22 00:10

Bharat Gulati


Swift 3 version:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    if theView.bounds.contains(touch.location(in: theView)) {
        return false
    }
    return true
}
like image 39
zgjie Avatar answered Oct 13 '22 01:10

zgjie


Ran into a similar issue; ended up using the answer from @Rob. Here's a Swift version:

extension ViewController: UIGestureRecognizerDelegate {
    func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
        return !CGRectContainsPoint(menuView.bounds, touch.locationInView(menuView))
    }
}
like image 39
Robert Chen Avatar answered Oct 13 '22 01:10

Robert Chen


If you want to disable UITapGestureRecognizer for a particular view, you just remove userInteraction.

Ex

sidePanel.userInteractionEnabled = NO;
like image 21
thavasidurai Avatar answered Oct 13 '22 01:10

thavasidurai


I have did this,with help of

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

and in that i have checked for touch point location & according to touch location i did my work like this

if(points.x>86)
    {//hide the side panel
     }

It recognizes gestures with synchronize with events.

like image 38
Ravindra Bagale Avatar answered Oct 12 '22 23:10

Ravindra Bagale