检测iPhone屏幕方向 [英] Detect iPhone screen orientation
问题描述
我想以45度的增量检测iPhone的方向。理想情况下,我希望能够沿任意轴获得方向角。
I'm wanting to detect the orientation of the iPhone at 45 degree increments. Ideally I'd like to be able to get the angle of orientation along any axis.
我需要做的检测类似于iPhone的Trism如何闪烁箭头当方向改变时,朝向屏幕的当前底部位置。
The detection I need to do is similar to how Trism for the iPhone flashes an arrow towards the current bottom position of the screen when orientation changes.
我有一些编码但实际上不明白加速度计读数如何工作并且可以使用微调正确的方向。我当前的代码记录了当前的角度,但即使手机电量不足,我的读数每秒也会变化很多。
I have something coded up but really don't understand how the accelerometer readings work and could use a nudge in the right direction. My current code logs the current angle but even when the phone is flat I get readings varying wildly a few times a second.
- (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);
}
电话持平时来自日志的样本:
Sample from log while phone is flat:
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
推荐答案
我认为你的问题是当你想要 float
时,你正在使用 int
变量。
I think your problem is that you're using int
variables when you want float
.
我认为 accelerationX
和-Y应该是实例变量而且是星期四s:
I think the accelerationX
and –Y should be instance variables and thus:
accelerationX = acceleration.x * kfilteringFactor + accelerationX * (1.0 - kfilteringFactor);
accelerationY = acceleration.y * kfilteringFactor + accelerationY * (1.0 - kfilteringFactor);
应该为您提供更多您想要的东西。
Should give you more what you were looking for.
这篇关于检测iPhone屏幕方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!