Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Tracking/Identifying individual touches on iPhone

DZone's Guide to

Tracking/Identifying individual touches on iPhone

· Mobile Zone
Free Resource

Get gorgeous, multi-touch charts for your iOS application with just a few lines of code.

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;
      }
protected:
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
                       stored_touch->update_position(touchPoint.x,touchPoint.y);
                }
 
	}
}

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.

.Net developers: use Highcharts, the industry's leading interactive charting library, without writing a single line of JavaScript.

Topics:

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

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}