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

发布于 2024-08-30 12:25:00 字数 28 浏览 4 评论 0原文

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

How do I detect two fingers tap on the iPhone?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

奢望 2024-09-06 12:25:00

如果您可以面向 OS 3.2 或更高版本,则可以使用 UITapGestureRecognizer。它真的很容易使用:只需配置它并将其附加到视图即可。当执行手势时,它将触发gestureRecognizer目标的动作。

示例:

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]

.

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

If you can target OS 3.2 or above, you can use a UITapGestureRecognizer. It's really easy to use: just configure it and attach it to the view. When the gesture is performed, it'll fire the action of the gestureRecognizer's target.

Example:

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

Then you just implement a - (void) viewWasDoubleTapped:(id)sender method, and that will get invoked when [self view] gets double-tapped.

EDIT

I just realized you might be talking about detecting a single tap with two fingers. If that's the case, the you can do

[r setNumberOfTouchesRequired:2]

.

The primary advantage of this approach is that you don't have to make a custom view subclass

怪我太投入 2024-09-06 12:25:00

如果您的目标不是 3.2+:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([touches count] == 2) {
        //etc
    }
}

If you're not targeting 3.2+:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([touches count] == 2) {
        //etc
    }
}
拥抱影子 2024-09-06 12:25:00

multiTouchEnabled 属性设置为 YES

Set the multiTouchEnabled property to YES.

嗼ふ静 2024-09-06 12:25:00

如果您的要求允许,请使用 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;

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

If your requirements allow, use UITapGestureRecognizer. Otherwise, implement the following UIResponder methods in your custom UIView:

- (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;

Track throughout to see how many touches there were and whether or not they moved more than your tap/drag threshold. You must implement all four methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文