UITableView滑动手势与UITableViewCell滑动冲突

时间:2017-05-09 12:20:00

标签: ios objective-c uitableview uiswipegesturerecognizer swtableviewcell

以下是我在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];  

我正在使用SWTableViewCell左右(单击)gestureRecognisers 使用2根手指左/右滑动UITableView时,SWTableViewCell左右手势也会被触发。
如何制止冲突?

3 个答案:

答案 0 :(得分:1)

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

    return YES;
}

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

答案 1 :(得分:1)

可能的解决方案是在touchesBegan:方法中启用/禁用BOOl(SWTableViewCellTouch),如下所示。

- (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] ;}

希望这会对你有所帮助。

答案 2 :(得分:1)

1。UIGestureRecognizerDelegate

中实施UIViewController

2。设置leftSwipe.delegate = self;leftSwipe.delegate = self;

3. 现在检查UISwipeGesture中的委托方法是否有多少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; 
    }

我希望这能解决你的问题