1

我有一个 cutsom UITableViewCell 实现。我已经为 UIPanGestureRecognizer 注册了 UITableViewCell 的这个子类,我用它来向右或向左滑动单元格。

// 在 UITableViewCell 子类中:

 UIGestureRecognizer* recognizer = 
 [[UIPanGestureRecognizer alloc] initWithTarget:
 self   
 action:@selector(handlePan:)];

    recognizer.delegate = self;
    [self addGestureRecognizer:recognizer];
       recognizer.cancelsTouchesInView = NO;

现在,当用户在屏幕上“向上”滑动两根手指时,我想展示一个视图控制器。所以,我在 tableview 中添加了一个 UISwipeGestureRecognizer。

// 包含 tableview 引用的视图控制器中的代码。

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleViewsSwipe:)];

            [swipe setDirection:UISwipeGestureRecognizerDirectionUp];
            [swipe setDelaysTouchesBegan:NO];
            [[self tableView ]addGestureRecognizer:swipe];
            swipe.cancelsTouchesInView= YES;
            [swipe setNumberOfTouchesRequired:2];
            swipe.delegate = self;
            self.tableView.multipleTouchEnabled = YES;

但是当我在屏幕上用两根手指滑动时,会触发平移手势。我该如何解决这个问题?

4

1 回答 1

0

正如 sooper 所说,设置 maximumNumberOfTouches = 1 可能会起作用。

对于尝试同时处理 2 个手势识别器的其他人,它们都是 1 个触摸手势,我发现确保将此委托设置为是

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}

然后在手势识别器操作中,您可以检查某个翻译或您需要的任何内容并取消其中一个手势识别器。

如:

- (void)panSwipeRecognizer:(UIPanGestureRecognizer*)panRecognizer
{
    CGPoint translation = [panRecognizer translationInView:self.superview];

    if(panRecognizer.state == UIGestureRecognizerStateBegan)
    {
        if(fabsf(translation.x) < fabsf(translation.y))
        {
            //deactivate horizontal gesture recognizer
            panRecognizer.enabled = NO;
            panRecognizer.enabled = YES;
        }
        else //if(fabsf(translation.x) > fabsf(translation.y))
        {
            //deactivate vertical gesture recognizer
            otherGestureRecognizer.enabled = NO;
            otherGestureRecognizer.enabled = YES;
        }
    }
    //other statements like stateChanged and stateBegan
}
于 2013-12-19T23:29:54.730 回答