如何获得真实设备的方向?

发布于 2024-11-17 01:00:10 字数 176 浏览 2 评论 0原文

全部,
当我锁定屏幕方向时,我想使用[[UIDevice currentDevice]orientation]来获取屏幕方向,但它总是返回UIDeviceOrientationPortrait,所以,问题是我怎样才能得到真实设备的方向。
非常感谢。

All,
When i locked the screen orientation,i want use [[UIDevice currentDevice] orientation] to get screen's orientation ,but it always return UIDeviceOrientationPortrait, so, question is how can i get the real Device's orientation.
thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

抚笙 2024-11-24 01:00:10

确保您通过以下方式启用方向通知:

[[UIDevice currentDevice] beginGenerateDeviceOrientationNotifications];

如果仍然不起作用,您还可以使用状态栏来确定当前方向(如果您的 UI正在旋转)通过:

[[UIApplication sharedApplication] statusBarOrientation];

Make sure you're enabling orientation notifications via:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

If that still doesn't work, you can also use the status bar to figure out the current orientation (if your UI is rotating) via:

[[UIApplication sharedApplication] statusBarOrientation];

掌心的温暖 2024-11-24 01:00:10

如果您在模拟器中进行测试,请注意模拟器在返回准确的方向值方面很糟糕。在设备上进行测试。

If you're testing in the Simulator then be aware that the Simulator is terrible at returning accurate orientation values. Test on a device.

妞丶爷亲个 2024-11-24 01:00:10

您需要首先打开方向通知生成,按照 文档

您需要调用:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

然后您可以检查设备的方向。您需要告诉设备您已完成方向获取,以节省电池。您可以通过致电来做到这一点:

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

就个人而言,我对 使用状态栏的方向。这是因为有时(例如,当设备平放在表面上时)设备方向不会是“横向”或“纵向”。

You need to turn on orientation notification generation first, as per the documentation.

You need to call:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

Then you can check the device's orientation. You need to tell the device that you are done getting the orientation, to conserve battery. You do so by calling:

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

Personally, I've had better luck with using the status bar's orientation. This is because sometimes, (when the device is flat on a surface, for example) the device orientation won't be "Landscape" or "Portrait".

祁梦 2024-11-24 01:00:10

另一种解决方案是从 CoreMotion 库检测方向。它会修复纵向方向锁定:打开时的错误。
请参阅该文章:http://codrspace.com/yuvirajsinh/device-orientation-使用 coremotion 进行检测/

Another solution could be detect orientation from CoreMotion library. It would fix a bug when Portrait Orientation Lock: On.
Refer that article: http://codrspace.com/yuvirajsinh/device-orientation-detection-using-coremotion/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文