7

Following is the code I have written to put 2 finger swipe on 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 UITableView is swiped left/right using 2 fingers, SWTableViewCell left and right gestures are also fired after that.
How to stop the conflict ?

kb920
  • 3,039
  • 2
  • 33
  • 44
Nitish
  • 13,845
  • 28
  • 135
  • 263

3 Answers3

1
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    if (SWTableViewCellTouch) {
        SWTableViewCellTouch = NO
        return NO;
    }

    return YES;
}

when you touch the SWTableViewCell set a BOOL SWTableViewCellTouch to YES.

Nitish
  • 13,845
  • 28
  • 135
  • 263
Nisar Ahmad
  • 885
  • 1
  • 10
  • 25
  • This does help but still there appears to be a minor conflict. It seems like I have to swipe on the table hard so as to avoid conflict. – Nitish May 23 '17 at 07:04
1

The possible solution is enable/disable the BOOl (SWTableViewCellTouch) in the touchesBegan: method as 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 will help you.

Nisar Ahmad
  • 885
  • 1
  • 10
  • 25
1

1. Implement UIGestureRecognizerDelegate in your UIViewController

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

3. Now check if the in its Delegate Method if UISwipeGesture have how many 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; 
    }

i hope this will solve your problem

Dhiru
  • 3,040
  • 3
  • 25
  • 69