Tom Wilson
Tom Wilson

Reputation: 65

Tracking Multiple Touches on iPhone

I have been wrestling with multi-touch 'finger tracking' for a few weeks now. My background is in hardware and embedded systems programming, so I'm inexperienced with anything other than micro-controller programming. I achieved some success, but only when multiple touches start and finish at the same time. My project is a gestural control interface for audio hardware, so I need to track each touch in order to produce custom gestures.

This example shows how I extract each touch from [touches allObjects] in order to process them individually (I excluded the processing code, it's superfluous to this example)

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

touchesArray = [touches allObjects]; //Array Declared in header to hold multiple touches.

NSUInteger nNumTouches = [touchesArray count]; //Counts number of touches in Array
UITouch *touch = [touches anyObject];
CGPoint ptTouch;

for (int nTouch = 0;  nTouch < nNumTouches;  nTouch++)
{
    touch = [touchesArray objectAtIndex:nTouch];
    ptTouch = [touch locationInView:self.view];
{

Is there a more effective method of tracking each touch? I feel I've hit the end of my programming knowledge. Especially after trying to manipulate an NSMutable Array to no effect. I've also read, and failed to execute details from here: https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/multitouch_background/multitouch_background.html#//apple_ref/doc/uid/TP40009541-CH5-SW9

The response to this looks interesting, but I couldn't get it to work in context:

how to track multitouch events on iphone?

Thanks for your time, any advice would be much appreciated,

Tom.

Upvotes: 1

Views: 128

Answers (3)

J1soon
J1soon

Reputation: 357

The accepted answer is correct. However, for those who still haven't figure out how to implement the idea, see my answer here.

The basic concept is to store each UITouch ID in an array when touchesBegan:: is called, and later compare each ID with the touches on screen in touchesMoved:: event. This way, each finger can be paired with a single object, and be tracked along when panning.

Upvotes: 1

Putz1103
Putz1103

Reputation: 6211

Your problem with them all needing to be touchdown/up at the same time is due to the fact that [touches allObjects] does not give you an array of all the fingers on the screen at any given time, it gives you an array of all the touches that match the current event (down/moved/ended/cancelled).

If you want to do it manually without using any gesturecognizers then you can get the full array of all touch objects by accessing event.allTouches. That will give you a full array and you can manage where touches are at all times.

Upvotes: 0

Gavin
Gavin

Reputation: 8200

Use a UIGestureRecognizer such as a UITapGestureRecognizer or UIPanGestureRecognizer. It should make it easier for you to deal with the handling of multiple touches and touches that occur at different times. There are also other UIGestureRecognizers available that you might want to look at to get the behavior you want.

Upvotes: 0

Related Questions