开发者

How can I get the real device's orientation?

开发者 https://www.devze.com 2023-03-15 00:47 出处:网络
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

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.


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:

[[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".


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号