6

如何检测 iPhone 上的两根手指轻敲?

4

4 回答 4

12

如果您可以针对 OS 3.2 或更高版本,则可以使用UITapGestureRecognizer. 它非常易于使用:只需对其进行配置并将其附加到视图即可。当手势被执行时,它会触发手势识别器目标的动作。

例子:

UITapGestureRecognizer * r = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewWasDoubleTapped:)];
[r setNumberOfTapsRequired:2];
[[self view] addGestureRecognizer:r];
[r release];

然后你只需要实现一个- (void) viewWasDoubleTapped:(id)sender方法,当[self view]被双击时它就会被调用。

编辑

我刚刚意识到您可能在谈论用两根手指检测单击。如果是这种情况,你可以这样做

[r setNumberOfTouchesRequired:2]
.

这种方法的主要优点是您不必创建自定义视图子类

于 2010-04-25T04:10:47.130 回答
4

如果您的目标不是 3.2+:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([touches count] == 2) {
        //etc
    }
}
于 2010-04-25T04:07:27.840 回答
2

multiTouchEnabled属性设置为YES

于 2010-04-25T03:25:20.650 回答
0

如果您的要求允许,请使用 UITapGestureRecognizer。否则,在您的自定义 UIView 中实现以下 UIResponder 方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

全程跟踪以查看有多少次触摸以及它们的移动是否超过了您的点击/拖动阈值。您必须实现所有四种方法。

于 2010-04-25T04:02:45.790 回答