UITableView swipe gesture conflicts with UITableViewCell swipe
Asked Answered
O

3

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 ?

Overlooker answered 9/5, 2017 at 12:19 Comment(6)
if it can be solved by using gesture delegate, just have a look this past ans and figure out.Foggia
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; }Kendy
@HimanshuMoradiya : Done that already. Still facing the issue.Overlooker
@Overlooker did you upload any gif or video that show your problem .Kendy
@HimanshuMoradiya : Let me try.Overlooker
@Overlooker why dont you disable editing of rows in tableview. it will stop the swipe gesture of tableview to detect anything.Option
M
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.

Marsiella answered 9/5, 2017 at 13:52 Comment(1)
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.Overlooker
M
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.

Marsiella answered 23/5, 2017 at 10:13 Comment(0)
L
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

Lobectomy answered 29/5, 2017 at 15:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.