UITongPress上的UILongPressGestureRecognizer - 双重调用

时间:2011-10-07 14:00:46

标签: ios uitableview uigesturerecognizer

我在单元格中使用UILongPressGestureRecognizer。 我需要的是:当用户点击一个单元格1.0秒时,调用一个视图控制器。 如果用户点击该单元格,则另一个VC。

我可以通过使用UILongPressGestureRecognizer来实现这一点。但问题是调用viewController两次。

代码:

if (indexPath.section == 0 && indexPath.row == 1){
    UILongPressGestureRecognizer *longPressTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(memberListWithSearchOptions)];

    longPressTap.minimumPressDuration = 1.0;

    [cell addGestureRecognizer:longPressTap];
    [longPressTap release];
}

我认为我需要的是,在识别出LongPress后,禁用识别器,直到tableView再次出现在屏幕上。

我该怎么做?

谢谢,

RL

2 个答案:

答案 0 :(得分:7)

您可能需要做的是检查手势识别器的state属性,而不是禁用它,如果状态为UIGestureRecognizerStateBegan(或UIGestureRecognizerStateEnded),则只显示下一个视图控制器。

您需要更改方法以接受手势识别器作为参数(并更新@selector参数)并检查其状态:

UILongPressGestureRecognizer *longPressTap = 
    [[UILongPressGestureRecognizer alloc] initWithTarget:self 
        action:@selector(memberListWithSearchOptions:)];  //colon at end

//...

- (void)memberListWithSearchOptions:(UILongPressGestureRecognizer *)lpt
{
    if (lpt.state == UIGestureRecognizerStateBegan)
        //or check for UIGestureRecognizerStateEnded instead
    {
        //display view controller...
    }
}

答案 1 :(得分:-1)

您必须检查以下状态

-  (void)memberListWithSearchOptions:(UILongPressGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
    NSLog(@"UIGestureRecognizerStateEnded");
    //Do Whatever You want on End of Gesture
}
else if (sender.state == UIGestureRecognizerStateBegan){
    NSLog(@"UIGestureRecognizerStateBegan.");
    //Do Whatever You want on Began of Gesture
}