【发布时间】:2016-05-06 23:13:53
【问题描述】:
我试图检测用户何时将他们的 iPhone 撞到另一个物体上,以及何时他们只是摇晃他们的手机。我似乎无法让它按我想要的那样完美地工作,因为它要么记录了太多的颠簸,没有颠簸,或者认为摇晃就是颠簸。
有人可以看看我下面的代码并提供建议吗?我需要确保其中一种发生。
// SHAKING
- (void) motionEnded: (UIEventSubtype) motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake)
{
[self setNumberOfShakes: [self numberOfShakes] + 1];
[self reloadAllTapShakeData];
}
}
// TAPPING & BUMPING
- (void) setupAccelerometerMonitoring
{
[self setManager: [[CMMotionManager alloc] init]];
if ([[self manager] isDeviceMotionAvailable])
{
[[self manager] setDeviceMotionUpdateInterval: 0.02];
[[self manager] startDeviceMotionUpdatesToQueue: [NSOperationQueue mainQueue] withHandler: ^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error)
{
//NSLog(@"x = %f | y = %f | z = %f", [motion userAcceleration].x, [motion userAcceleration].y, [motion userAcceleration].z);
if (([motion userAcceleration].x > .50 && [motion userAcceleration].x < 1)
|| ([motion userAcceleration].y > .70 && [motion userAcceleration].x < 1)
|| ([motion userAcceleration].z > .80 && [motion userAcceleration].z < 1))
{
NSLog(@"TAPPED ON ANOTHER OBJECT");
}
}];
}
}
【问题讨论】:
标签: ios iphone core-motion