UITableViewcell和UITableVIew上的手势识别器

时间:2013-06-09 10:59:14

标签: ios uitableview uigesturerecognizer uiswipegesturerecognizer

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

//在UITableViewCell子类中:

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

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

现在我想在用户双手划动“向上”时呈现一个视图控制器 屏幕上。 所以,我在桌面视图中添加了一个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;

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

1 个答案:

答案 0 :(得分:0)

正如sooper所说,设置maximumNumberOfTouches = 1可能会有效。

对于其他尝试同时处理2个gestureRecognizers同时处理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
}