【问题标题】:Multitouch question多点触控问题
【发布时间】:2009-07-30 21:59:55
【问题描述】:

我想检测,当用户摇晃 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。在这种情况下,震动不算数——就好像屏幕上没有触摸一样。我假设当我将一只手从屏幕上移开时,两个“触摸”都会被移走。

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

谢谢。

【问题讨论】:

    标签: iphone cocoa-touch multi-touch


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2015-05-20
      • 1970-01-01
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多