UITouches“丢失”多点触控期间

发布于 2024-12-20 19:33:01 字数 1885 浏览 1 评论 0原文

我无法让 UIView 通过多次触摸来响应我想要的方式。基本上,某些 UITouch 处于 UITouchPhaseBegan 状态,但从未进入 UITouchPhaseEndedUITouchPhaseCancelled 状态。这是我用来处理触摸的代码,它是从 touchesBegan:withEventtouchesMoved:withEventtouchesEnded: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 UITouches 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千里故人稀 2024-12-27 19:33:01

一个事件可以报告多次触摸。所以有时你会得到“结束!”一次,因为只有一个事件到达并且只调用了一次触摸事件处理程序 - 但它报告了两次触摸都结束了。如果您手动处理多个同时发生的触摸(手指),则需要您单独跟踪每个触摸并检查每个事件中的每个触摸,以了解报告了多少个触摸并决定要做什么。

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".)

花辞树 2024-12-27 19:33:01

刚刚尝试了您的代码,其中存在一些问题。
有时我会得到两个手指的“Began Began Began End End”,因为 touchesBegan 被调用两次,第一次有一个开始触摸,第二次有两个开始触摸。

我不知道你为什么不拆分方法并将代码放入 touchesBegantouchesMovedtouchesEnded 方法中。但您应该使用从参数传递的 touches,而不是 [event allTouches]

- (void) handleTouches:(NSSet *)touches {
    for( UITouch* touch in touches ) {
        if( touch.phase == UITouchPhaseBegan ) {
            NSLog(@"Began!");
        }
        else if( touch.phase == UITouchPhaseMoved ) {

        }
        else if( touch.phase == UITouchPhaseEnded || touch.phase == UITouchPhaseCancelled ) {
            NSLog(@"Ended!");
        }
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self handleTouches:touches];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [self handleTouches:touches];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self handleTouches:touches];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [self handleTouches:touches];
}

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 use touches that passed from the argument instead of [event allTouches].

- (void) handleTouches:(NSSet *)touches {
    for( UITouch* touch in touches ) {
        if( touch.phase == UITouchPhaseBegan ) {
            NSLog(@"Began!");
        }
        else if( touch.phase == UITouchPhaseMoved ) {

        }
        else if( touch.phase == UITouchPhaseEnded || touch.phase == UITouchPhaseCancelled ) {
            NSLog(@"Ended!");
        }
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self handleTouches:touches];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [self handleTouches:touches];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self handleTouches:touches];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [self handleTouches:touches];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文