15

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

aheze
  • 24,434
  • 8
  • 68
  • 125
Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70

6 Answers6

28

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;
    }
    
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • how to bound viewcontroller? – Gami Nilesh Oct 27 '15 at 11:33
  • I don't understand your question. – Rob Oct 27 '15 at 15:19
  • you write this code: self.menuView.bounds ,but how do i bound Demomenuviewcontroller -https://github.com/romaonthego/REFrostedViewController – Gami Nilesh Oct 28 '15 at 01:33
  • @GamiNilesh - I'd suggest you post your own question, show us how you're using that code, and give us a [reproducible example](http://stackoverflow.com/help/mcve). I'm not at all clear what you're trying to say, so perhaps you can describe your situation in greater detail in your own question, rather than here in comments. – Rob Oct 28 '15 at 06:56
16

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;
Bharat Gulati
  • 796
  • 6
  • 12
  • @RavindraBagale Did you set the gesture recognizer's `delegate` to be the view controller? Bharat's answer is correct. – Rob Feb 09 '13 at 14:58
10

Swift 3 version:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    if theView.bounds.contains(touch.location(in: theView)) {
        return false
    }
    return true
}
zgjie
  • 2,099
  • 2
  • 21
  • 32
5

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))
    }
}
Robert Chen
  • 5,179
  • 3
  • 34
  • 21
3

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

Ex

sidePanel.userInteractionEnabled = NO;
thavasidurai
  • 1,972
  • 1
  • 26
  • 51
1

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.

Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70