CoreMotion Bump与iPhone上的摇晃

Jud*_*phy 6 iphone ios core-motion

我试图检测用户何时将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");
        }
    }];
}
Run Code Online (Sandbox Code Playgroud)

}

Hem*_*ang -1

我确信这是一个不同的答案,但这可能会对您有所帮助。

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;
     }
 }];
Run Code Online (Sandbox Code Playgroud)

如需完整示例,请查看 git。