经过一番挖掘,我找到了一个巧妙的解决方案,并得到了这个人的大力帮助(http://www.codedojo.com/?p=1030)。
最后,它是关于了解 iOS 如何处理其触摸事件。我最初不清楚的是,当触摸开始时,会创建一个 UITouch 对象,然后只要该手指向下(即使您在屏幕上移动它)iOS 使用相同的 UITouch 对象,但会更新对象基于您的操作。
这是我实现的基础,使用 codedojo 的想法,我实现了一个简单的 touchManager,它维护一个 UITouch 对象数组,并在手指从屏幕上添加或移除时更新这个数组。
因此,在任何时候,您都有一个数据结构来维护所有 UITouch 对象,您可以使用这些对象获取多次触摸的手指轨迹。最重要的是,您可以在代码中的任何位置使用此数据结构来非常轻松地执行必要的操作。
@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];
}
这对我来说效果很好,我可以在 iPad 上跟踪多达 11 个手指!