iOS - 检测何时有多个手指在屏幕上
我正在寻找同时检测屏幕上多个手指的最佳方法。我没有检测到轻击或捏合,只是检测到不止一次触摸发生。似乎没有任何手势识别器。最好的办法是什么?
I'm looking for the best way to detect more than one finger on the screen at time. I'm not detecting taps or pinching, just the fact that more than one touch is happening. There don't seem to be any gesture recognizers for that. What's the best way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在touchesBegan、touchesMoved、touchesEnded方法中,有一个参数是event,它是一个UIEvent对象。屏幕上的手指数量为[[event allTouches]count]。
[已编辑,因为 Josh Hinman 指出我之前犯了错误——我之前建议在这些相同的方法中对 Touches 参数使用 [touches count] 是行不通的。]
In the touchesBegan, touchesMoved, and touchesEnded methods, one parameter is event, which is a UIEvent object. The number of fingers on the screen is [[event allTouches]count].
[EDITED because Josh Hinman pointed out that I had it wrong before -- my previous suggestion of using [touches count] on the touches parameter in those same methods will not work.]
阅读
-touchesBegan:withEvent:
方法。它是多点触摸事件处理的入口点。以下是有关多点触控事件的开发人员的库链接:
https://developer.apple.com/库/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MultitouchEvents/MultitouchEvents.html
Read up on the
-touchesBegan:withEvent:
method. It's the entry point into multi-touch event handling.Here's a developer's lib link on multitouch events:
https://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MultitouchEvents/MultitouchEvents.html
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"%lu",[[事件 allTouches] 计数]);
(void) touchesBegan
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"%lu",[[event allTouches] count]);
}
单点/多点触摸对您来说是透明的 - 您只需收到有关触摸开始/移动/结束/点击位置的通知。如果同时发生两次触摸,您将收到两次触摸的通知。
我不知道有什么内置函数可以确定您看到的触摸序列是否由捏合组成 - 但您可以查看 Apple 的“触摸”示例代码以获取灵感。
https://developer.apple.com/library/ios/#samplecode/Touches/
One/Multi touch is transparent to you - You just get notifications on where a touch started/moved/ended/tapped. If two touches occur at the same time, you will get notifications for both.
I don't know of any built-in function which determines whether or not the touch sequences you see consist of a pinch - But you can take a look at the "touches" sample code from Apple for inspiration.
https://developer.apple.com/library/ios/#samplecode/Touches/
您可以尝试使用 UITapGestureRecognizer 类,并设置 numberOfTouchesRequired 属性设置为 2。
请注意,这仅在 multipleTouchEnabled 在视图上设置为 YES。
You can try using a UITapGestureRecognizer class, and set the numberOfTouchesRequired property to 2.
Note that this will only work if multipleTouchEnabled is set to YES on the view.