如果用户单次触摸,我想执行一个操作,如果用户在UITableView Cell上进行双击,我想执行另一个操作。
我尝试了这个问题中提到的多种方法。
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");
}
}
这样做的正确方法是什么?
答案 0 :(得分:2)
我希望您在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];
答案 1 :(得分:0)
根据答案 - 您的单击将处理定时器点火方法。 点击此处点按
- (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;
这是完全有效的代码段。