【问题标题】:UITouches "lost" during multitouchUITouches 在多点触控期间“丢失”
【发布时间】:2011-12-13 02:58:05
【问题描述】:

我无法获得UIView 以通过多次触摸来响应我想要的方式。基本上确定UITouches 在UITouchPhaseBegan,但永远不会到达UITouchPhaseEndedUITouchPhaseCancelled。这是我用来处理触摸的代码,从touchesBegan:withEventtouchesMoved:withEventtouchesEnded:withEventtouchesCancelled: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 );
        }
    }
}

编辑:

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

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

【问题讨论】:

  • 为什么你使用[event allTouches] 而不是touches 传递自touchesBegan:withEvent

标签: ios uiview multi-touch uitouch touches


【解决方案1】:

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

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

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

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

【讨论】:

    【解决方案2】:

    刚试了你的代码,有一些问题。 我有时会因为两个手指而得到“开始开始开始结束结束”,因为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];
    }
    

    【讨论】:

    • 我在移动视图时使用了类似的东西。效果很好。
    猜你喜欢
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    • 2012-04-19
    • 1970-01-01
    • 2014-01-12
    • 2014-08-31
    • 2011-07-07
    • 1970-01-01
    相关资源
    最近更新 更多