【问题标题】:Issue in detecting double tap on a UITableViewCell检测 UITableViewCell 双击时的问题
【发布时间】:2015-07-01 05:45:45
【问题描述】:

如果用户进行了一次触摸,我想执行一个操作,如果用户在 UITableView 单元上进行了两次触摸,我想执行另一个操作。

我尝试了这个问题中提到的多种方法。

How can I detect a double tap on a certain cell in UITableView?

但是每种方法,我都无法正确区分单击和双击。我的意思是,在每次双击中,它也会发生一次单击。所以,双击发生,每次单击动作也会触发。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
FeedCell *myCell = (FeedCell*) [self.tblView cellForRowAtIndexPath:indexPath];

NSLog(@"clicks:%d", myCell.numberOfClicks);

if (myCell.numberOfClicks == 2) {
    NSLog(@"Double clicked");
}
else{

    NSLog(@"Single tap");
}
}  

这样做的正确方法应该是什么?

【问题讨论】:

  • 使用长按手势
  • 在哪里增加numberOfClicks的值?你在哪里捕捉到双击?
  • 在 CustomTableViewCell 中
  • - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *aTouch = [touches anyObject]; self.numberOfClicks = [aTouch tapCount]; [超级 touchesEnded:touches withEvent:event]; }

标签: ios objective-c iphone uitableview


【解决方案1】:

我宁愿不要使用 didSelectRowAtIndexPath 而你想要 double tap 操作。使用 single TapGesture 替换为 didSelectRowAtIndexPath。无论您在didSelectRowAtIndexPath 中编写的任何代码,都将在single tap 选择器方法中编写。

示例:实现单双手势,如下所示。

UITapGestureRecognizer *singleTap = [[[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(doSingleTap)] autorelease];
singleTap.numberOfTapsRequired = 1; 
[self.view addGestureRecognizer:singleTap];

UITapGestureRecognizer *doubleTap = [[[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(doDoubleTap)] autorelease];
doubleTap.numberOfTapsRequired = 2; 
[self.view addGestureRecognizer:doubleTap];

[singleTap requireGestureRecognizerToFail:doubleTap];

【讨论】:

  • 简单方法。为我工作。谢谢
  • 它甚至可以在没有额外复杂性的 MapView 上工作,例如计时器、额外的全局变量等等......
【解决方案2】:

根据答案-您的单击将处理计时器触发方法。 将您的单击操作放在这里

- (void)tapTimerFired:(NSTimer *)aTimer{
    //timer fired, there was a single tap on indexPath.row = tappedRow
    if(tapTimer != nil){
        tapCount = 0;
        tappedRow = -1;
    }
}

双击将在didSelectRowAtIndexPath 中处理,如图所示:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //checking for double taps here
    if(tapCount == 1 && tapTimer != nil && tappedRow == indexPath.row){
        //double tap - Put your double tap code here
        [tapTimer invalidate];
        [self setTapTimer:nil];
    }
    else if(tapCount == 0){
        //This is the first tap. If there is no tap till tapTimer is fired, it is a single tap
        tapCount = tapCount + 1;
        tappedRow = indexPath.row;
        [self setTapTimer:[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(tapTimerFired:) userInfo:nil repeats:NO]];
    }
    else if(tappedRow != indexPath.row){
        //tap on new row
        tapCount = 0;
        if(tapTimer != nil){
            [tapTimer invalidate];
            [self setTapTimer:nil];
        }
    }
}

你只需要声明两个属性

@property (nonatomic, assign) NSInteger tapCount;
@property(nonatomic, assign) NSInteger tappedRow;

这是完全正常的sn-p。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    相关资源
    最近更新 更多