title: iPhone: Tracking/Identifying individual touches
body: <p>I have a quick question regarding tracking touches on the iPhone and I seem to not be able to come to a conclusion on this, so any suggestions / ideas are greatly appreciated:</p>
  
  <p>I want to be able to track and identify touches on the iphone, ie. basically every touch has a starting position and a current/moved position. Touches are stored in a std::vector and they shall be removed from the container, once they ended. Their position shall be updated once they move, but I still want to keep track of where they initially started (gesture recognition).</p>
  
  <p>I am getting the touches from [event allTouches], thing is, the NSSet is unsorted and I seem not to be able to identify the touches that are already stored in the std::vector and refer to the touches in the NSSet (so I know which ones ended and shall be removed, or have been moved, etc.)</p>
  
  <p>Here is my code, which works perfectly with only one finger on the touch screen, of course, but with more than one, I do get unpredictable results...</p>
  
  <pre><code>    - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
  {
      [self handleTouches:[event allTouches]];
  }
  
  - (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
  {
      [self handleTouches:[event allTouches]];
  }
  
  - (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
  {
      [self handleTouches:[event allTouches]];
  }
  
  - (void) touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event
  {
      [self handleTouches:[event allTouches]];
  }
  
  - (void) handleTouches:(NSSet*)allTouches
  {   
      for(int i = 0; i &lt; (int)[allTouches count]; ++i)
      {
          UITouch* touch = [[allTouches allObjects] objectAtIndex:i];
          NSTimeInterval timestamp = [touch timestamp];
  
          CGPoint currentLocation = [touch locationInView:self];
          CGPoint previousLocation = [touch previousLocationInView:self];
  
          if([touch phase] == UITouchPhaseBegan)
          {
              Finger finger;
              finger.start.x = currentLocation.x;
              finger.start.y = currentLocation.y;
              finger.end = finger.start;
              finger.hasMoved = false;
              finger.hasEnded = false;
  
              touchScreen-&gt;AddFinger(finger);
          }
          else if([touch phase] == UITouchPhaseEnded || [touch phase] == UITouchPhaseCancelled)
          {
              Finger&amp; finger = touchScreen-&gt;GetFingerHandle(i);
  
              finger.hasEnded = true;
          }
          else if([touch phase] == UITouchPhaseMoved)
          {
              Finger&amp; finger = touchScreen-&gt;GetFingerHandle(i);
  
              finger.end.x = currentLocation.x;
              finger.end.y = currentLocation.y;
              finger.hasMoved = true;
          }
      }
  
      touchScreen-&gt;RemoveEnded();
  }
  </code></pre>
  
  <p>Thanks!</p>