UITableView swipe gesture conflicts with UITableViewCell swipe

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 ?


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

    return YES;
}

当您触摸SWTableViewCell时,将BOOL SWTableViewCellTouch设置为YES。


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.


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

链接地址: http://www.djcxy.com/p/39392.html

上一篇: 更改当前sbt任务范围中的变量

下一篇: UITableView轻扫手势与UITableViewCell轻扫冲突