如何获得真实设备的方向?
全部,
当我锁定屏幕方向时,我想使用[[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
确保您通过以下方式启用方向通知:
[[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];
如果您在模拟器中进行测试,请注意模拟器在返回准确的方向值方面很糟糕。在设备上进行测试。
If you're testing in the Simulator then be aware that the Simulator is terrible at returning accurate orientation values. Test on a device.
您需要首先打开方向通知生成,按照 文档。
您需要调用:
然后您可以检查设备的方向。您需要告诉设备您已完成方向获取,以节省电池。您可以通过致电来做到这一点:
就个人而言,我对 使用状态栏的方向。这是因为有时(例如,当设备平放在表面上时)设备方向不会是“横向”或“纵向”。
You need to turn on orientation notification generation first, as per the documentation.
You need to call:
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:
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".
另一种解决方案是从 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/