由UIDeviceOrientationDidChangeNotification返回的设备方向不准确(Inaccurate device orientation returned by UIDevi

编程入门 行业动态 更新时间:2024-10-28 18:26:13
由UIDeviceOrientationDidChangeNotification返回的设备方向不准确(Inaccurate device orientation returned by UIDeviceOrientationDidChangeNotification)

我正在收听UIDeviceOrientationDidChangeNotification,以便根据设备的方向调整我的UI。

问题是我从通知中获得的设备方向似乎不准确。 如果我以纵向和垂直方向(如同拍摄照片一样)从手机开始,则从通知中获得的方向是正确的。 随着手机的倾斜接近水平(如平躺在桌面上),方向切换到横向。 这种情况发生在手机实际平放在桌上之前。 而这完全没有将手机转向风景。 就好像它对风景有偏好。

当使用其他应用程序时,例如邮件,我不会看到相同的行为。 看来邮件只有在确定你已经进入新的方向后才会切换方向。

任何帮助不胜感激。

谢谢,

I'm listening to the UIDeviceOrientationDidChangeNotification in order to adjust my UI based on the device's orientation.

The problem is that the device orientation I'm getting from the notification seems inaccurate. If I start with the phone in portrait orientation and vertical (as if taking a picture with it) the orientation I get from the notification is correct. As the tilt of the phone approaches horizontal (as in laying flat on a table top) the orientation switches to landscape. This happens much before the phone is actually flat on the table. And this is without rotating the phone towards landscape at all. Is as if it had a preference for landscape.

When using other apps, like mail, I don't see this same behavior. It seems that mail only switches orientation once it's really sure you've gone to the new orientation.

Any help greatly appreciated.

Thanks,

最满意答案

如果您的唯一兴趣是您的界面方向(即设备的横向或纵向),则不应使用UIDevice:orientation(或如果您愿意,则使用UIDeviceOrientation *常量),而应使用UIApplication:statusBarOrientation,它使用UIInterfaceOrientation *常量。

我使用下面的代码来检查横向模式:

static inline bool isOrientationIsLandscape() { return UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]); }

对于肖像模式:

static inline bool isOrientationIsPortrait() { return UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]); }

这花了我整整一个上午的时间,因为在模拟器上没有UIDeviceOrientationFace [Up | Down],只能在真实的设备上使用。 所以我的应用程序一直在模拟器上工作,但在实际的设备上,我在测试过程中偶尔会遇到一些未定义的行为。

现在它在仿真器上的实际设备上工作。

HTH

If your only interest is your interface orientation (ie landscape or portrait orientation of the device), you should NOT use UIDevice:orientation (or the UIDeviceOrientation* constants if you will), but rather use the UIApplication:statusBarOrientation, which uses the UIInterfaceOrientation* constants.

I use the following code to check for landscape modus:

static inline bool isOrientationIsLandscape() { return UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]); }

And for portrait modus:

static inline bool isOrientationIsPortrait() { return UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]); }

This cost me a whole morning to figure out, because there is no UIDeviceOrientationFace[Up|Down] on the simulator, only on the real device. So my app worked on the simulator all the time, but on the actual device I had some undefined behavoir during testing every now and then.

Now it works on the actual device like it does on the simulator.

HTH

更多推荐

本文发布于:2023-07-15 11:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1112841.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不准确   方向   设备   UIDeviceOrientationDidChangeNotification   returned

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!