开发者

iPad orientation returns unknown values

I use this code:

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

to get开发者_StackOverflow iPad orientation but it returns UIDeviceOrientationUnknown when I use OpenGL rendering. When I play movies or I display UIViews it return the correct value. Do you know how may I get correct orientation? I don't want to use the accelerometer because I want to know when the device has locked orientation.

Thank you!


There are situations where the accelerometer can’t get an accurate read on the orientation, such as when the device is held flat. You should still be able to get the current orientation using:

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];


In iOS 13, the statusBarOrientation has been deprecated. The new preferred way of getting this orientation is

let orientation = UIApplication.shared.windows.first?.windowScene?.interfaceOrientation


If I add [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; the orientation will be correct even in OpenGL rendering mode. UIDeviceOrientationUnknown orientation is sometimes returned but only for short time.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜