Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differentiating Between UITouch Objects On The iPhone

I'm trying to differentiate between two (or more) UITouch objects on the iPhone. Specifically, I'd like to know the order in which the touches occurred.

For instance, in my -touchesBegan:withEvent: method I get an NSSet of UITouch objects. Now I can find out how many touches there are, but, which object represents which finger?

I notice the timestamp property on UITouch - is this what I'm looking for? I see how that would be useful to obtaining the last or first touch - providing the touches don't mutate...

Therein lies my problem. I can use the timestamp to single out the latest touch, but then the touch that occurred first moves, and IT becomes the latest touch...

At the end of this exercise, I'd like to be able to implement the "pinch" gesture to zoom in or out, etc.

Any help would be greatly appreciated, thanks.

like image 253
Jasarien Avatar asked Jun 13 '09 01:06

Jasarien


3 Answers

Each finger gets one UITouch object, and the same object is given to you again and again. Simply remember the pointers to set of initial UITouch objects, and do a pointer comparison to keep track of fingers.

From Apple's reference docs:

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.

like image 109
Dave R Avatar answered Sep 21 '22 06:09

Dave R


I've got some touch sample code here:

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

It's been useful to some people, and I think it might help you out.

Take a look at the NSMutableArray *ActiveTouches. It's the collection I use to keep track of the order in which touches occur.

There is an example of pinch and stretch in that code base the uses that array.

like image 39
amattn Avatar answered Sep 22 '22 06:09

amattn


The often-discussed solution is to watch the events and compare the current x and y and previous x and y values to match them up. There seem to be some tricks though. Events can come out of order, and I've seen cases where it looked like the hardware or OS was getting confused with lots of (10) moving fingers.

A few people pointed out that the touch events are reused and you can use that fact to keep track of which finger is which. This has been covered at least once on StackOverflow, I believe.

like image 32
Nosredna Avatar answered Sep 19 '22 06:09

Nosredna