Over a million developers have joined DZone.

Tracking/Identifying individual touches on iPhone

· Mobile Zone

I got this question the other day, how to track individual touches if you are using a couple of fingers for a game?

Actually it’s pretty easy if you can access the touch previous position and it’s current position.

Let’s start with a small class to store the information

class Touch
public: Touch(); ~Touch();
      void update_position(float x, float y){ _current_position.x = x; _current_position.x = y; }
      bool is_same_touch(float x, float y)
            if(_current_position.x == x && _current_position.y == y) return true; 
            return false;
CGPoint _current_position;

Let’s a assume the “move” movement

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
        // Loop all touches
	for (UITouch *touch in touches)
                // Get current position in view
		CGPoint touchPoint = [touch locationInView:self];
                // Get previous position
		CGPoint prevPoint = [touch previousLocationInView:self];		
                // compare with your stored touch
                if(stored_touch->is_same_touch(prevPoint.x, prevPoint.y))
                       // Do stuff with the movement
                       // Update

As you can see you now have for each touch it’s current and previous position. So by storing each touch you can later compare it’s current position with the new touch old position.


Published at DZone with permission of David Amador, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}