Ios “UITouches”;“丢失”;在多点触摸中

Ios “UITouches”;“丢失”;在多点触摸中,ios,uiview,multi-touch,uitouch,touches,Ios,Uiview,Multi Touch,Uitouch,Touches,我在获取ui视图时遇到问题,无法通过多次触摸来响应我的要求。基本上,某些UITouche处于UITouchPhaseBegan中,但从未进入uitouchPhaseEneed或uitouchPhaseCanced。下面是我用来处理触摸的代码,从touchesbeated:withEvent,touchesMoved:withEvent,touchesend:withEvent和touchescanceled:withEvent调用。如果我放下一根手指,然后再放下另一根手指,移动它们,同时释放它们

我在获取
ui视图
时遇到问题,无法通过多次触摸来响应我的要求。基本上,某些
UITouch
e处于
UITouchPhaseBegan
中,但从未进入
uitouchPhaseEneed
uitouchPhaseCanced
。下面是我用来处理触摸的代码,从
touchesbeated:withEvent
touchesMoved:withEvent
touchesend:withEvent
touchescanceled: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 );
        }
    }
}
编辑:

我悬赏是因为我真的想要一个好的解决方案。总而言之:我需要一个系统,在这个系统中,开始的每一次触摸都会结束,因此,如果用户放下一根手指,然后把另一根手指放在其他地方,我可以看到两次触摸都开始了,当手指不再与设备接触时,我已经看到两次触摸都结束了


我是否采取了错误的策略来实现这一点?

一个事件可能会报告多次接触。因此,您有时会得到一次“结束!”,因为只有一个事件到达,并且只调用了一个touch事件处理程序-但它报告了两个touch都已结束。如果您手动处理多个同时触摸(手指),则由您单独跟踪每个触摸,并检查每个事件中的每个触摸,以查看报告的触摸数量,并决定如何操作

Apple的示例代码显示了如何通过维护CFDictionaryRef来实现这一点:


(向下滚动到“处理多点触控事件”部分)

刚刚尝试了您的代码,但出现了一些问题。 我有时会用两个手指“开始结束”,因为
touchsbegind
被呼叫两次,第一次有一个开始触摸,第二次有两个开始触摸

我不知道您为什么不拆分该方法,并将代码放入
touchsbegind
touchsmoved
touchsended
方法中。但是您应该使用从参数传递的
触摸
,而不是
[事件所有触摸]

- (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];
}

为什么使用
[event alltouchs]
而不是从
touchsbegind:withEvent
传递的
touchs
?我在移动视图时使用了类似的方法。它工作得很好。