gpt4 book ai didi

iPhone:跟踪/识别个人触摸

转载 作者:行者123 更新时间:2023-12-03 18:24:52 30 4
gpt4 key购买 nike

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

谢谢!

最佳答案

看来跟踪多个触摸的“正确”方法是通过 UITouch 事件的指针值。

您可以在本文的“处理复杂的多点触控序列”部分中找到更多详细信息 Apple Developer Documentation

关于iPhone:跟踪/识别个人触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/913086/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com