UITouches“丢失”多点触控期间
我无法让 UIView
通过多次触摸来响应我想要的方式。基本上,某些 UITouch
处于 UITouchPhaseBegan
状态,但从未进入 UITouchPhaseEnded
或 UITouchPhaseCancelled
状态。这是我用来处理触摸的代码,它是从 touchesBegan:withEvent
、touchesMoved:withEvent
、touchesEnded:withEvent
和 调用的>touchesCancelled:withEvent
。如果我放下一根手指,然后另一根手指,移动它们,然后同时释放它们,NSLog
输出有时会开始!开始了!结束!而不是开始!开始了!结束了!结束了!。这些接触是否在某个地方丢失了?我怎样才能跟踪它们?
- (void) handleTouchEvent:(UIEvent *)event {
for( UITouch* touch in [event allTouches] ) {
if( touch.phase == UITouchPhaseBegan ) {
NSLog(@"Began!");
if( ![m_pCurrentTouches containsObject:touch] )
[m_pCurrentTouches addObject:touch];
uint iVoice= [m_pCurrentTouches indexOfObject:touch];
CGPoint location = [touch locationInView:self];
m_pTouchPad->SetTouchPoint( location.x, location.y, iVoice );
m_pTouchPad->SetIsTouching( true, iVoice );
}
else if( touch.phase == UITouchPhaseMoved ) {
uint index= [m_pCurrentTouches indexOfObject:touch];
CGPoint location = [touch locationInView:self];
m_pTouchPad->SetTouchPoint( location.x, location.y, index );
}
else if( touch.phase == UITouchPhaseEnded || touch.phase == UITouchPhaseCancelled ) {
uint index= [m_pCurrentTouches indexOfObject:touch];
[m_pCurrentTouches removeObject:touch];
NSLog(@"Ended!");
m_pTouchPad->SetIsTouching( false, index );
}
}
}
编辑:
我提供赏金是因为我真的想要一个好的解决方案。总结一下:我需要一个系统,其中每次开始的触摸都会结束,因此,如果用户放下一根手指,然后在其他地方放下另一根手指,我可以看到两次触摸都开始,并且到没有手指时当我不再与设备接触时,我已经看到两次接触都结束了。
我是否采取了错误的策略来实现这一目标?
I'm having trouble getting a UIView
to respond how I want with multiple touches. Basically certain UITouch
es are in UITouchPhaseBegan
but never make it to UITouchPhaseEnded
or UITouchPhaseCancelled
. Here's the code I'm using to handle touches, which is called from touchesBegan:withEvent
, touchesMoved:withEvent
, touchesEnded:withEvent
and touchesCancelled:withEvent
. If I put one finger down, then another, move them, and release them simultaneously, the NSLog
output is sometimes Began! Began! Ended! rather than Began! Began! Ended! Ended!. Are these touches getting lost somewhere? How can I keep track of them?
- (void) handleTouchEvent:(UIEvent *)event {
for( UITouch* touch in [event allTouches] ) {
if( touch.phase == UITouchPhaseBegan ) {
NSLog(@"Began!");
if( ![m_pCurrentTouches containsObject:touch] )
[m_pCurrentTouches addObject:touch];
uint iVoice= [m_pCurrentTouches indexOfObject:touch];
CGPoint location = [touch locationInView:self];
m_pTouchPad->SetTouchPoint( location.x, location.y, iVoice );
m_pTouchPad->SetIsTouching( true, iVoice );
}
else if( touch.phase == UITouchPhaseMoved ) {
uint index= [m_pCurrentTouches indexOfObject:touch];
CGPoint location = [touch locationInView:self];
m_pTouchPad->SetTouchPoint( location.x, location.y, index );
}
else if( touch.phase == UITouchPhaseEnded || touch.phase == UITouchPhaseCancelled ) {
uint index= [m_pCurrentTouches indexOfObject:touch];
[m_pCurrentTouches removeObject:touch];
NSLog(@"Ended!");
m_pTouchPad->SetIsTouching( false, index );
}
}
}
EDIT:
I'm offering a bounty because I really want a good solution to this. To summarize: I need a system where every touch that begins also ends, so if a user puts down one finger and then another elsewhere, I can see both touches begin, and by the time there are no fingers in contact with the device anymore, I have seen both touches end.
Am I pursuing the wrong strategy to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个事件可以报告多次触摸。所以有时你会得到“结束!”一次,因为只有一个事件到达并且只调用了一次触摸事件处理程序 - 但它报告了两次触摸都结束了。如果您手动处理多个同时发生的触摸(手指),则需要您单独跟踪每个触摸并检查每个事件中的每个触摸,以了解报告了多少个触摸并决定要做什么。
Apple 有示例代码,展示了如何通过维护 CFDictionaryRef 来做到这一点:
http://developer.apple.com/library/IOs/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MultitouchEvents/MultitouchEvents.html#//apple_ref/doc/uid/TP40009541-CH3-SW7
(向下滚动到“处理多点触控事件”部分。)
One event can report many touches. So you are sometimes getting "Ended!" once, because only one event arrived and only one touch event handler call was made - but it reported both touches ending. If you are manually handling multiple simultaneous touches (fingers), it is up to you to track each touch individually and check every touch in every event to see how many of your touches are being reported and decide what to do.
Apple has sample code showing how to do this by maintaining a CFDictionaryRef:
http://developer.apple.com/library/IOs/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MultitouchEvents/MultitouchEvents.html#//apple_ref/doc/uid/TP40009541-CH3-SW7
(Scroll down to the section called "Handling Multitouch Events".)
刚刚尝试了您的代码,其中存在一些问题。
有时我会得到两个手指的“Began Began Began End End”,因为
touchesBegan
被调用两次,第一次有一个开始触摸,第二次有两个开始触摸。我不知道你为什么不拆分方法并将代码放入
touchesBegan
、touchesMoved
、touchesEnded
方法中。但您应该使用从参数传递的touches
,而不是[event allTouches]
。Just tried your code, which have some problems.
I get "Began Began Began End End" for two fingers sometimes because
touchesBegan
get called two times and first time have one began touch second time have two began touches.I don't know why you didn't split the method and put the code into the
touchesBegan
,touchesMoved
,touchesEnded
methods. But you should usetouches
that passed from the argument instead of[event allTouches]
.