关于在iPhone上跟踪触摸操作,我有一个问题。我似乎无法得出结论,因此非常感谢任何建议/想法:
我想能够跟踪和识别iPhone上的触摸操作,即基本上每个触摸都有一个起始位置和当前/移动位置。触摸存储在std :: vector中,一旦它们结束,它们将从容器中删除。一旦它们移动,它们的位置应该更新,但我仍然想跟踪它们最初的位置(手势识别)。
我正在从[event allTouches]获取触摸,问题是,NSSet是无序的,我似乎无法识别已经存储在std :: vector中的触摸,并引用NSSet中的触摸(这样我就知道哪些触摸已经结束并且应该被删除,或者已经被移动等等)
以下是我的代码,当然,在触摸屏上只有一个手指时,它可以完美运行,但是当有多个手指时,我会得到不可预测的结果......
- (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 < (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->AddFinger(finger);
}
else if([touch phase] == UITouchPhaseEnded || [touch phase] == UITouchPhaseCancelled)
{
Finger& finger = touchScreen->GetFingerHandle(i);
finger.hasEnded = true;
}
else if([touch phase] == UITouchPhaseMoved)
{
Finger& finger = touchScreen->GetFingerHandle(i);
finger.end.x = currentLocation.x;
finger.end.y = currentLocation.y;
finger.hasMoved = true;
}
}
touchScreen->RemoveEnded();
}
谢谢!