ios - 如何在 iOS 上检测设备的方向?

标签 ios objective-c orientation uidevice

我有一个关于如何在 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/

相关文章:

ios - 使用 SKShapeNode 绘制和删除线条

ios - AVCaptureVideoDataOutputSampleBufferDelegate 使用 CIFilters 进行丢帧进行视频过滤

ios - 是否有快速绘制二维数组的推荐方法?

objective-c - 无法快速访问 FMDatabaseAdditions 方法?

ImageMagick 不根据 EXIF 旋转图像

ios - NSTimer 不调用选择器或按时间间隔重复

ios - 如何在 UITextView 中控制/隐藏快捷操作

ios - 有没有办法比较CGPoints?

UIImagePickerController 返回错误的图像方向

android - 变量切换方向丢失