Tracking/Identifying individual touches on iPhone
Join the DZone community and get the full member experience.
Join For FreeI 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.
Published at DZone with permission of David Amador, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments