문제

사용자가 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을 흔들고 싶다. 이 경우 화면에 터치가없는 것처럼 쉐이크는 계산되지 않습니다. 화면에서 한 손을 제거하면 두 "터치"가 제거된다고 가정합니다.

문제는 무엇이며 어떻게 해결할 수 있습니까?

감사.

도움이 되었습니까?

해결책

왜 당신은 열거하고 있습니까? -allTouches? 그냥 열거합니다 touches 전달 된 세트. 두 방법 모두 동일하게됩니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top