【发布时间】: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