8

我试图区分 iPhone 上的两个(或更多) UITouch 对象。具体来说,我想知道触摸发生的顺序。

例如,在我的 -touchesBegan:withEvent: 方法中,我得到一个 UITouch 对象的 NSSet。现在我可以找出有多少次触摸,但是,哪个对象代表哪个手指?

我注意到 UITouch 上的 timestamp 属性——这就是我要找的吗?我知道这对于获得最后一次或第一次触摸有什么用 - 前提是触摸不会发生变化......

这就是我的问题。我可以使用时间戳来挑出最新的触摸,但是首先发生的触摸移动,IT 成为最新的触摸......

在本练习结束时,我希望能够实现“捏合”手势来放大或缩小等。

任何帮助将不胜感激,谢谢。

4

6 回答 6

24

每根手指得到一个 UITouch 对象,同一个对象一次又一次地给你。只需记住指向初始 UITouch 对象集的指针,并进行指针比较以跟踪手指。

来自 Apple 的参考文档:

A touch object is persistent for a given finger during a sequence, and UIKit mutates it as it tracks the finger throughout it. The touch attributes that change are the phase of the touch, its location in a view, its previous location, and its timestamp. Event-handling code evaluates these attributes to determine how to respond to the event.

于 2009-06-13T07:11:01.750 回答
3

我在这里有一些触摸示例代码:

http://github.com/kailoa/6tringle-touchsamplecode/tree/master http://6tringle.com/blog/2009/TouchSampleCode.html

它对某些人很有用,我认为它可能会对您有所帮助。

看看NSMutableArray *ActiveTouches。这是我用来跟踪触摸发生顺序的集合。

该代码库中有一个使用该数组的捏和拉伸示例。

于 2009-06-13T02:31:27.257 回答
2

经常讨论的解决方案是观察事件并比较当前的 x 和 y 以及之前的 x 和 y 值以匹配它们。不过好像有一些技巧。事件可能会出现乱序,我见过一些情况,看起来硬件或操作系统与许多 (10) 个移动手指混淆了。

一些人指出,触摸事件被重用,您可以使用这一事实来跟踪哪个手指是哪个。我相信,这在 StackOverflow 上至少已经讨论过一次。

于 2009-06-13T01:32:39.647 回答
2

I had a look at the Kailoa Kadano's code. You basically keep a NSMutableArray of current UITouch objects. However the documentation of UITouch states this:

A UITouch object is persistent throughout a multi-touch sequence. You should never retain an UITouch object when handling an event. If you need to keep information about a touch from one phase to another, you should copy that information from the UITouch object.

I don't quite see the reason for not retaining it if you release it again when the event finished but that's what the SDK doc says.

于 2009-10-22T13:49:53.880 回答
2

I was looking for the same info. And yes it appears you're not suppose to retain the UITouch data. According to the "Handling a Complex Multi-Touch Sequence" section of this Apple Developer Documentation, the "proper" way to track multiple touches is by the pointer value of the UITouch event.

于 2010-03-28T07:21:23.333 回答
1

看看我给出的这些答案,这可能会有所帮助:

接触 UIResponder API

iPhone:跟踪/识别个人触摸

于 2009-06-13T03:31:07.380 回答