我有一个关于跟踪 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();
}
谢谢!