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

我有一个关于如何在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
    
如果UIViewController:
if (UIDeviceOrientationIsLandscape(self.interfaceOrientation))
{
    // 
}
如果UIView:
if (UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
{
    //
}
UIDevice.h:
#define UIDeviceOrientationIsPortrait(orientation)  ((orientation) == UIDeviceOrientationPortrait || (orientation) == UIDeviceOrientationPortraitUpsideDown)
#define UIDeviceOrientationIsLandscape(orientation) ((orientation) == UIDeviceOrientationLandscapeLeft || (orientation) == UIDeviceOrientationLandscapeRight)
更新: 将此代码添加到xxx-Prefix.pch,然后您可以在任何地方使用它:
// check device orientation
#define dDeviceOrientation [[UIDevice currentDevice] orientation]
#define isPortrait  UIDeviceOrientationIsPortrait(dDeviceOrientation)
#define isLandscape UIDeviceOrientationIsLandscape(dDeviceOrientation)
#define isFaceUp    dDeviceOrientation == UIDeviceOrientationFaceUp   ? YES : NO
#define isFaceDown  dDeviceOrientation == UIDeviceOrientationFaceDown ? YES : NO
用法:
if (isLandscape) { NSLog(@"Landscape"); }
    
对于您首先要寻找的内容,如果方向发生变化,您必须获得通知! 你可以在viewDidLoad中设置This Thing
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(OrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
每当您的设备的方向改变时,OrientationDidChange被调用,您可以按照每个方向执行任何操作
-(void)OrientationDidChange:(NSNotification*)notification
{
    UIDeviceOrientation Orientation=[[UIDevice currentDevice]orientation];

    if(Orientation==UIDeviceOrientationLandscapeLeft || Orientation==UIDeviceOrientationLandscapeRight)
    {
    }
    else if(Orientation==UIDeviceOrientationPortrait)
    {
    }
}
    
如果您想直接从加速度计获取设备方向,请使用
[[UIDevice currentDevice] orientation]
。但是,如果您需要应用程序的当前方向(界面方向),请使用
[[UIApplication sharedApplication] statusBarOrientation]
。     
UIViewController
具有
interfaceOrientation
属性,您可以访问该属性以查找视图控制器的当前方向。 至于你的例子,这应该工作。当你说它不起作用时,你的意思是什么?它给你的结果与你的预期结果有什么关系?     
  在Swift 3.0中 获取设备方向。
/* return current device orientation.
   This will return UIDeviceOrientationUnknown unless device orientation notifications are being generated. 
*/
UIDevice.current.orientation
从您的应用程序获取设备方向
UIApplication.shared.statusBarOrientation
    
“UIDeviceOrientation”不满意,因为当UIViewcontroller方向固定到特定方向时,您没有获得与设备方向相关的信息,因此正确的做法是使用“UIInterfaceOrientation”。 您可以使用“self.interfaceOrientation”从UIViewController获取方向,但是当您分解我们的代码时,您可能需要在视图控制器之外进行此类测试(自定义视图,类别......),因此您仍然可以可以使用rootviewController访问控制器外部的任何信息:
if (UIInterfaceOrientationIsLandscape(view.window.rootViewController.interfaceOrientation)) {
}
    
无论是否通过使用CoreMotion中的数据启用方向锁,都可以实现此目的。 这是代码:
#import <CoreMotion/CoreMotion.h> 

    CMMotionManager *cm=[[CMMotionManager alloc] init];
    cm.deviceMotionUpdateInterval=0.2f;
    [cm startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
                            withHandler:^(CMDeviceMotion *data, NSError *error) {

                            if(fabs(data.gravity.x)>fabs(data.gravity.y)){
                                    NSLog(@"LANSCAPE");
                                if(data.gravity.x>=0){
                                    NSLog(@"LEFT");
                                }
                                else{
                                    NSLog(@"RIGHT");
                                }

                        }
                        else{
                                NSLog(@"PORTRAIT");
                                if(data.gravity.y>=0){
                                    NSLog(@"DOWN");
                                }
                                else{

                                    NSLog(@"UP");
                                }

                            }

}];
    
您是否已解锁硬件锁以进行设备定位? iPad 1的边缘有一个。     
这里有一些Swift变量可以使检测变得更容易:
let LANDSCAPE_RIGHT: Bool = UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight
let LANDSCAPE_LEFT: Bool = UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft
let LANDSCAPE: Bool = LANDSCAPE_LEFT || LANDSCAPE_RIGHT
let PORTRAIT_NORMAL: Bool = UIDevice.currentDevice().orientation == UIDeviceOrientation.Portrait
let PORTRAIT_REVERSE: Bool = UIDevice.currentDevice().orientation == UIDeviceOrientation.PortraitUpsideDown
let PORTRAIT: Bool = PORTRAIT_REVERSE || PORTRAIT_NORMAL
    

要回复问题请先登录注册