我想检测当用户摇动 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。在这种情况下,震动不算数——就好像屏幕上没有触摸一样。我假设当我从屏幕上移开一只手时,两个“触摸”都被移走了。
有什么问题,我该如何解决?
谢谢。