我正在尝试检测用户何时将 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");
}
}];
}
}
我确信这是一个不同的答案,但这可能会对您有所帮助。
https://github.com/bumptech/bump-api-ios https://github.com/bumptech/bump-api-ios
它有一个块功能,例如
[[BumpClient sharedClient] setBumpEventBlock:^(bump_event event) {
switch(event) {
case BUMP_EVENT_BUMP:
NSLog(@"Bump detected.");
break;
case BUMP_EVENT_NO_MATCH:
NSLog(@"No match.");
break;
}
}];
如需完整示例,请查看 git。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)