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:
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).
You should accept Bharat's answer because that is correct. I only want to illustrate how you do it:
Define your view controller as conforming to UIGestureRecognizerDelegate
, e.g.:
@interface ViewController () <UIGestureRecognizerDelegate>
// the rest of your interface
@end
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];
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;
}
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;
Swift 3 version:
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
if theView.bounds.contains(touch.location(in: theView)) {
return false
}
return true
}
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))
}
}
If you want to disable UITapGestureRecognizer
for a particular view, you just remove userInteraction.
Ex
sidePanel.userInteractionEnabled = NO;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With