Ravindra Bagale
Ravindra Bagale

Reputation: 17655

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

Upvotes: 15

Views: 19915

Answers (6)

zgjie
zgjie

Reputation: 2276

Swift 3 version:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    if theView.bounds.contains(touch.location(in: theView)) {
        return false
    }
    return true
}

Upvotes: 10

Robert Chen
Robert Chen

Reputation: 5355

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))
    }
}

Upvotes: 5

Rob
Rob

Reputation: 437552

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;
    }
    

Upvotes: 28

Ravindra Bagale
Ravindra Bagale

Reputation: 17655

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.

Upvotes: 1

thavasidurai
thavasidurai

Reputation: 2002

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

Ex

sidePanel.userInteractionEnabled = NO;

Upvotes: 3

Bharat Gulati
Bharat Gulati

Reputation: 796

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;

Upvotes: 16

Related Questions