iPhone上的CoreMotion Bump vs. Shake

我试图检测用户碰撞另一个物体的时间与刚刚抖动手机的时间。 我似乎无法像我想要的那样完美地工作,因为它要么登记过多的颠簸,要么没有颠簸,要么认为摇动是一个颠簸。

有人可以看看我的代码,并提供build议吗? 我需要确定一个或另一个发生。

// 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

它有一个像块一样的function

 [[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。