1

我无法UIView通过多次触摸来响应我想要的方式。基本上某些UITouches 在UITouchPhaseBegan但永远不会进入UITouchPhaseEndedor UITouchPhaseCancelled。这是我用来处理触摸的代码,从、touchesBegan:withEvent和调用。如果我将一根手指向下,然后另一根手指,同时移动它们并释放它们,输出有时是Began!开始了!结束了!而不是开始!开始了!结束了!结束了!. 这些触摸是否在某个地方丢失了?我怎样才能跟踪他们?touchesMoved:withEventtouchesEnded:withEventtouchesCancelled:withEventNSLog

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

编辑:

我提供赏金是因为我真的想要一个好的解决方案。总结一下:我需要一个系统,每次开始的触摸都会结束,所以如果用户放下一根手指,然后在别处放下另一根手指,我可以看到两个触摸都开始了,当没有手指与设备接触时,我已经看到这两种接触都结束了。

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

4

2 回答 2

1

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

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

http://developer.apple.com/library/IOs/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MultitouchEvents/MultitouchEvents.html#//apple_ref/doc/uid/TP40009541-CH3-SW7

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

于 2012-01-10T00:25:45.257 回答
1

刚试了你的代码,有一些问题。有时我会用两个手指得到“开始开始开始结束结束”,因为touchesBegan被调用两次,第一次开始触摸第二次开始触摸两次。

我不知道你为什么不拆分方法并将代码放入touchesBegan, touchesMoved,touchesEnded方法中。但是您应该使用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];
}
于 2012-01-10T00:42:58.177 回答