iPad方向返回未知值

Mir*_*pas 6 ipad

我用这个代码:

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

获取iPad方向,但在使用OpenGL渲染时返回UIDeviceOrientationUnknown.当我播放电影或显示UIViews时,它会返回正确的值.你知道我怎么能得到正确的方向?我不想使用加速度计,因为我想知道设备何时锁定方向.

谢谢!

Jef*_*ley 19

在某些情况下,加速度计无法准确读取方向,例如当设备保持平坦时.您仍然可以使用以下方式获取当前方向:

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
Run Code Online (Sandbox Code Playgroud)


Bek*_*Bek 5

在 iOS 13 中,statusBarOrientation已弃用。获得此方向的新首选方法是

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


Mir*_*pas 1

如果我添加 [[UIDevice currentDevice] beginGenerateDeviceOrientationNotifications]; 即使在 OpenGL 渲染模式下,方向也是正确的。UIDeviceOrientationUnknown 方向有时会返回,但时间很短。