我有一个关于如何在 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)