代码之家  ›  专栏  ›  技术社区  ›  Ilya Suzdalnitski

多点触摸问题

  •  1
  • Ilya Suzdalnitski  · 技术社区  · 15 年前

    我想知道,当用户摇动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;
            }
        }
    }
    

    如果用户在进行另一次握手之前移除双手,一切都会正常工作,但是如果我的双手放在屏幕上并移除其中一只,一切都会变得混乱。

    也就是说,我在屏幕上用双手握手,然后我只想用一只手握手。在这种情况下,震动不算数——就好像屏幕上没有触摸一样。我假设当我从屏幕上取下一只手时,两个“触摸”都会被移除。

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

    谢谢。

    1 回复  |  直到 15 年前
        1
  •  2
  •   kperryua    15 年前

    你为什么要列举 -allTouches ?只需列举 touches 传入的集合。这两种方法也是如此。