我有一个关于如何在 iOS 上检测设备方向的问题。我不需要接收更改通知,只需要接收当前方向本身。这似乎是一个相当简单的问题,但我一直无法理解它。以下是我到目前为止所做的:
UIDevice *myDevice = [UIDevice currentDevice] ;
[myDevice beginGeneratingDeviceOrientationNotifications];
UIDeviceOrientation deviceOrientation = myDevice.orientation;
BOOL isCurrentlyLandscapeView = UIDeviceOrientationIsLandscape(deviceOrientation);
[myDevice endGeneratingDeviceOrientationNotifications];
在我看来,这应该可行。我让设备接收设备方向通知,然后询问它的方向,但它不工作,我不知道为什么。
最佳答案
真正的旧线程,但没有真正的解决方案。
我遇到了同样的问题,但发现获取 UIDeviceOrientation 并不总是一致的,所以改用这个:
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == 0) //Default orientation
//UI is in Default (Portrait) -- this is really a just a failsafe.
else if(orientation == UIInterfaceOrientationPortrait)
//Do something if the orientation is in Portrait
else if(orientation == UIInterfaceOrientationLandscapeLeft)
// Do something if Left
else if(orientation == UIInterfaceOrientationLandscapeRight)
//Do something if right
关于ios - 如何在 iOS 上检测设备的方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5559652/