Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Tap Gesture on Child View Controller? How to determine Is it a PanGesture or TapGesture?

All.

In my iOS 8.0 app.

In a Parent Child View architecture. By this code...

[self addChildViewController:slideTableViewController];
[self.view addSubview:slideTableViewController.view];
[slideTableViewController didMoveToParentViewController:self];

I have implemented TapGesturerecognizer & PanGesturerecognizer on Base View Controller.

So, that it can recognise Pan(Dragging) and Tap. I need both gestures on my BaseView.

Just do not want Tap Gesture on SlideView. As I want to execute didSelectRowAtIndexpath method on child view,

Solution:

Answer for Question 1: Many StackOverflow answers have the same funda.. Disable Tap gesture when your touch encounters child view.

if([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]){
                if(CGRectContainsPoint(slideTableViewController.view.frame,[touch locationInView:self.view])){
                    return NO;
     }
}

Answer for Question 2:

How to determine Is it a PanGesture or TapGesture ?

For each gesture type the delegate method will call

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

If you have implemented two gestures in your view, pan gesture and touch gesture,

This method will call 2 times, 1 time for pan gesture and 1 time for tap gesture.

So, in this method you can check like isKindOfClass method.

Thank you very much for helping......

enter image description here

like image 258
Arpit B Parekh Avatar asked Feb 09 '23 17:02

Arpit B Parekh


2 Answers

Just set tapGesture.cancelsTouchesInView = YES;

like image 53
arturdev Avatar answered Feb 26 '23 09:02

arturdev


You can implement the gesture's delegate method in your baseViewController :

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizershouldReceiveTouch:(UITouch *)touch {
    return touch.view == self.view;
}

OR //If it is a Tap Gesture and Your touch intersects with a perticular View, For that we have this method.

if([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]){
                if(CGRectContainsPoint(slideTableViewController.view.frame,[touch locationInView:self.view])){
                    return NO;
     }
}
like image 44
Bannings Avatar answered Feb 26 '23 09:02

Bannings