質問

Im trying to enable multitouch gesture recognition for my application. I have been trying to maintain a data structure that would maintain touch traces (all the points in the life time of a touch from start to move to end) for multiple fingers.

It seems like the UIResponder methods gives only sets of touches that are active , moving or have ended at a particular point in time with no relation between the touches that have moved and the touches that started the move. Is there a way to establish this relationship, or am i missing something?

Any ideas on how i can data structure will also be helpful, or ideas on how i can get extremely fast multi touch response for gesture traces on my application will be helpful!

NOTE : I remember seeing a blog once which had a neat way to maintain pointers to touches started by multiple fingers and id-ing them using integers for each finger and use this to get a fast multi-touch response (not able to find it online now though!)

役に立ちましたか?

解決

After some digging around i found a neat solution to this, with a lot of help from this guy (http://www.codedojo.com/?p=1030).

In the end it was about understanding how iOS handles its touch events. What was not initially clear to me is that when a touch begins a UITouch object is created and then as long as that finger is down (even if you are moving it around the screen) iOS uses the same UITouch object but updates the properties of the object based on your action.

This was the basis for my implementation and using the idea from codedojo i implemented a simple touchManager that maintains an Array of UITouch objects and updates this array whenever a finger is added or removed from screen.

So at any point in time you have a data structure that maintains all the UITouch objects using which you can get the finger traces for multiple touches. And the big up is, you can use this data struct anywhere in your code to do what is necessary pretty easily.

@interface TouchStateManager : NSObject{
    NSMutableArray *touches;
 }

@property(nonatomic , assign , readwrite)NSMutableArray *touches;


-(int)addTouch:(UITouch *)_touch;
-(int)getActiveTouchCount;
-(int)getFingerIdForTouch:(UITouch *)_touch;



@implementation GETouchStateManager
@synthesize touches;


-(int)getFingerIdForTouch:(UITouch *)_touch{

    if(self.touches == NULL){
        self.touches = [[NSMutableArray alloc] initWithCapacity:MAX_TOUCHES];
      }
    if([self.touches containsObject:_touch]){
        return [self.touches indexOfObject:_touch];
     }
    else
        return -1;
}

-(int)addTouch:(UITouch *)_touch{
    if([self.touches count] == 0){
        [self.touches addObject:_touch];
        return [self getActiveTouchCount];
}

   else{
     if(![self.touches containsObject:_touch]){
         [self.touches addObject:_touch];
         return [self getActiveTouchCount];
    }

}

return -1;
}

-(int)getActiveTouchCount{
   DLog(@"Count : %d" , [touches count]);
   return [touches count];
}

This worked pretty well for me, im able to track upto 11 fingers in the iPad!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top