UITableView conflicts gestures using UITableViewCell

Below is the code I wrote to put 2 fingers on a UITableView :

 UISwipeGestureRecognizer *leftSwipe = [UISwipeGestureRecognizer new]; [leftSwipe addTarget:self action:@selector(nextDay)]; leftSwipe.numberOfTouchesRequired = 2; leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft; leftSwipe.delegate = self; [leftSwipe setCancelsTouchesInView:YES]; [tableViewTasks addGestureRecognizer:leftSwipe]; UISwipeGestureRecognizer *rightSwipe = [UISwipeGestureRecognizer new]; [rightSwipe addTarget:self action:@selector(previousDay)]; rightSwipe.numberOfTouchesRequired = 2; rightSwipe.direction = UISwipeGestureRecognizerDirectionRight; rightSwipe.delegate = self; [rightSwipe setCancelsTouchesInView:YES]; [tableViewTasks addGestureRecognizer:rightSwipe]; 

I am using SWTableViewCell which has left and right (single tap) gestureRecognisers.
When the UITableView scrolls left / right using 2 fingers, then the left and right gestures are also triggered by SWTableViewCell .
How to stop the conflict?

+7
ios objective-c uitableview uiswipegesturerecognizer
source share
3 answers
 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ if (SWTableViewCellTouch) { SWTableViewCellTouch = NO return NO; } return YES; } 

when you touch SWTableViewCell, set BOOL SWTableViewCellTouch to YES.

+1
source share

A possible solution is to enable / disable BOOl (SWTableViewCellTouch) in the touchhesBegan method: as shown below.

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if ([[event touchesForView:self] count] > 1) { // Its two finger touch so set the BOOL false like SWTableViewCellTouch = NO; } else if ([[event touchesForView:self] count] == 1){ // Its sigle finger touch so set the BOOL true like SWTableViewCellTouch = YES; } [super touchesBegan:touches withEvent:event] ;} 

Hope this helps you.

+1
source share

1. Add a UIGestureRecognizerDelegate to the UIViewController

2. Set leftSwipe.delegate = self; and leftSwipe.delegate = self;

3. Now check if there is a delegation in his method if UISwipeGesture has a numberOfTouchesRequired

  -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { if ( [gestureRecognizer isMemberOfClass:[UISwipeGestureRecognizer class]] ) { UISwipeGestureRecognizer *swipeGesture=(UISwipeGestureRecognizer *)gestureRecognizer ; if(swipeGesture.numberOfTouchesRequired!=2) { //if Double not Double Swipe Touch Don't Linsten Gesture in your Viewcontroller return NO; } } return YES; } 

Hope this solves your problem.

+1
source share

All Articles