CoreMotion Bump vs. Shake on iPhone

你离开我真会死。 提交于 2019-12-08 19:47:38

问题


I am trying to detect when a user bumps their iPhone on another object versus when they just shake their phone. I can't seem to get it to work perfectly as I want it, because it either registers too many bumps, no bumps, or thinks a shake is a bump.

Can someone look at my code below and offer suggestions? I need to be sure one or the other happens.

// 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");
        }
    }];
}

}


回答1:


I am sure this is a different answer but this may help you.

https://github.com/bumptech/bump-api-ios

It has a block function like

[[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;
     }
 }];

For complete example checkout the git.



来源:https://stackoverflow.com/questions/37082685/coremotion-bump-vs-shake-on-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!