我想以 45 度的增量检测 iPhone 的方向。理想情况下,我希望能够获得沿任何轴的方向角。
我需要做的检测类似于当方向改变时,iPhone 的 Trism 如何向屏幕的当前底部位置闪烁一个箭头。
我有一些编码但真的不明白加速度计读数是如何工作的,并且可以在正确的方向上使用轻推。我当前的代码记录了当前的角度,但即使手机是平的,我也会得到每秒几次剧烈变化的读数。
- (void) checkOrientation:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
int accelerationX = acceleration.x * kfilteringFactor + accelerationX * (1.0 - kfilteringFactor);
int accelerationY = acceleration.y * kfilteringFactor + accelerationY * (1.0 - kfilteringFactor);
float currentRawReading = (atan2(accelerationY, accelerationX)) * 180/M_PI;
NSLog(@"Angle: %f",currentRawReading);
}
手机平放时从日志中采样:
2009-06-16 17:29:07.987 [373:207] Angle: 0.162292
2009-06-16 17:29:07.994 [373:207] Angle: 179.838547
2009-06-16 17:29:08.014 [373:207] Angle: 179.836182
2009-06-16 17:29:08.032 [373:207] Angle: -90.000000
2009-06-16 17:29:08.046 [373:207] Angle: 179.890900
2009-06-16 17:29:08.059 [373:207] Angle: -90.000000
2009-06-16 17:29:08.074 [373:207] Angle: 179.917908
2009-06-16 17:29:08.088 [373:207] Angle: -179.950424
2009-06-16 17:29:08.106 [373:207] Angle: 90.000000
2009-06-16 17:29:08.119 [373:207] Angle: 90.000000
2009-06-16 17:29:08.134 [373:207] Angle: -179.720245