检测 iPhone 屏幕方向 [英] Detect iPhone screen orientation

查看:20
本文介绍了检测 iPhone 屏幕方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以 45 度的增量检测 iPhone 的方向.理想情况下,我希望能够获得沿任何轴的方向角度.

我需要做的检测类似于 iPhone 的 Trism 如何在方向改变时向屏幕的当前底部位置闪烁箭头.

我有一些编码,但真的不明白加速度计读数是如何工作的,可以在正确的方向上使用微调.我当前的代码记录了当前的角度,但即使手机是平的,我的读数也会每秒发生几次剧烈变化.

- (void) checkOrientation:(UIAccelerometer*)accelerator didAccelerate:(UIAcceleration*)acceleration{int 加速度X = 加速度.x * kfilteringFactor + 加速度X * (1.0 - kfilteringFactor);int 加速度Y = 加速度.y * kfilteringFactor + 加速度Y * (1.0 - kfilteringFactor);float currentRawReading = (atan2(accelerationY,accelerationX)) * 180/M_PI;NSLog(@"角度:%f",currentRawReading);}

手机平放时的日志样本:

2009-06-16 17:29:07.987 [373:207] 角度:0.1622922009-06-16 17:29:07.994 [373:207] 角度:179.8385472009-06-16 17:29:08.014 [373:207] 角度:179.8361822009-06-16 17:29:08.032 [373:207] 角度:-90.0000002009-06-16 17:29:08.046 [373:207] 角度:179.8909002009-06-16 17:29:08.059 [373:207] 角度:-90.0000002009-06-16 17:29:08.074 [373:207] 角度:179.9179082009-06-16 17:29:08.088 [373:207] 角度:-179.9504242009-06-16 17:29:08.106 [373:207] 角度:90.0000002009-06-16 17:29:08.119 [373:207] 角度:90.0000002009-06-16 17:29:08.134 [373:207] 角度:-179.720245

解决方案

我认为您的问题是您在需要 float 时使用了 int 变量.

我认为 accelerationX 和 –Y 应该是实例变量,因此:

accelerationX = 加速度.x * kfilteringFactor +accelerationX * (1.0 - kfilteringFactor);加速度Y = 加速度.y * kfilteringFactor + 加速度Y * (1.0 - kfilteringFactor);

应该给你更多你想要的东西.

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.

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

解决方案

I think your problem is that you're using int variables when you want float.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆