2

我说的是用同一根手指在屏幕上进行两次单独的触摸

我想我有这个编码权。但我几乎不可能在我的 iPad 上实际生成双击。是否可以增加单击和双击之间的时间间隔,以便更容易触发。我非常快速地双击并捕获它作为两次单击。只有有时我幸运地触发了双击。我将 UIButton 项的子类放入滚动视图中。

无论如何,我的 UIButton 子类实现:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{

 UITouch *touch = [[event allTouches] anyObject];

 NSLog(@"Touch count:%d",touch.tapCount);

 if (touch.tapCount == 1) 
 {
  //Do things for one touch
 }
 else if (touch.tapCount == 2) 
 {
  //Do things for double touch
 }
}

这是捕捉事件。这就是为什么我认为我的代码是正确的,我只是找不到与 UIEvent 相关的任何内容以及决定发生多少次触摸的原因。我在不同的 UIView 中测试了同样的东西,它完全按预期工作。

4

3 回答 3

2

您的代码同时检测视图中的两个手指,而不是手指上的快速序列(双击)。

如果要检测双击,最简单的方法是使用手势识别器。无论在哪里设置您的子类,请添加以下代码:

UITapGestureRecognizer *singleFingerDoubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerDoubleTap:)];
singleFingerDoubleTap.numberOfTouchesRequired = 1;
singleFingerDoubleTap.numberOfTapsRequired = 2;
[self addGestureRecognizer:singleFingerDTap];
[singleFingerDTap release];

并实现一个方法来处理双击:

-(void)handleSingleFingerDoubleTap:(id)sender {
  //Do stuff
}
于 2010-09-09T19:50:56.093 回答
0

你不打电话[super touchesBegan:touches withEvent:event]

于 2010-09-09T19:57:30.720 回答
0

这篇文章有一个我认为正确答案的粗略草图:在 UIButton 中双击

于 2010-11-16T04:26:20.580 回答