多点触控问题

时间:2009-07-30 21:59:56

标签: iphone cocoa-touch multi-touch

当用户摇动iPhone时,我想检测屏幕的哪个部分被触摸。

我是按照以下方式做的:

-(void) accelerometer: (UIAccelerometer*)accelerometer didAccelerate: (UIAcceleration*)acceleration
{
    float shakeStrength = sqrt( acceleration.x * acceleration.x + acceleration.y * acceleration.y + acceleration.z * acceleration.z );

    if (shakeStrength >= 1.5f)
    {
        if (isLeftHandTouches && isRightHandTouches)
        {
            DebugLog(@"both hands shake");
        } else if (isLeftHandTouches)
        {
            DebugLog(@"left hand shake");
        } else if (isRightHandTouches)
        {
            DebugLog(@"right hand shake");
        }
    }
}

-(void) touchesBegan: (NSSet *)touches withEvent: (UIEvent *)event
{
    NSSet *allTouches = [event allTouches];

    for (int i = 0; i < [allTouches count]; i++)
    {
        if ([ [ [allTouches allObjects] objectAtIndex: i] locationInView: [self view] ].x <= 240.0f)
        {
            isLeftHandTouches = YES;
        } else
        {
            isRightHandTouches = YES;
        }
    }
}

-(void) touchesEnded: (NSSet *)touches withEvent: (UIEvent *)event
{
    NSSet *allTouches = [event allTouches];

    for (int i = 0; i < [allTouches count]; i++)
    {
        if ([ [ [allTouches allObjects] objectAtIndex: i] locationInView: [self view] ].x <= 240.0f)
        {
            isLeftHandTouches = NO;
        } else
        {
            isRightHandTouches = NO;
        }
    }
}

如果用户在再次摇动前移开双手,一切正常,但如果我双手放在屏幕上并移除其中一个,则一切都会搞砸。

即。我用双手在屏幕上摇晃,之后我想用一只手摇动iPhone。在这种情况下,摇动不会计数 - 就好像屏幕上没有触摸一样。我假设当我从屏幕上移开一只手时,两个“触摸”都被删除了。

问题是什么,我该如何解决?

感谢。

1 个答案:

答案 0 :(得分:2)

为什么要枚举-allTouches?只需枚举传入的touches集。两种方法都是一样的。