How Do I detect the orientation of the device on iOS?

后端 未结 12 869
小鲜肉
小鲜肉 2020-11-30 01:56

I have a question on how to detect the device orientation on iOS. I don\'t need to receive change notifications, just the current orientation itself. This seems to be a rath

相关标签:
12条回答
  • 2020-11-30 02:47

    There's a way to achieve this whether the orientation lock is enabled or not by using data from CoreMotion. This is the code:

    #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");
                                    }
    
                                }
    
    }];
    
    0 讨论(0)
  • 2020-11-30 02:48

    In Swift 3.0

    to get device orientation.

    /* return current device orientation.
       This will return UIDeviceOrientationUnknown unless device orientation notifications are being generated. 
    */
    UIDevice.current.orientation
    

    to get device orientation from your app

    UIApplication.shared.statusBarOrientation
    
    0 讨论(0)
  • 2020-11-30 02:49

    And the best reliable way in swift :

    public extension UIScreen {
    
        public class var isPortrait: Bool {
            UIApplication.shared.delegate?.window??.rootViewController?.interfaceOrientation.isPortrait ??
                    UIApplication.shared.statusBarOrientation.isPortrait
        }
    
        public class var isLandscape: Bool { !isPortrait }
    }
    
    0 讨论(0)
  • 2020-11-30 02:52

    My current way of doing this:

    + (BOOL)isPortrait {
        let window = UIApplication.sharedApplication.delegate.window;
        if(window.rootViewController) {
            let orientation =
            window.rootViewController.interfaceOrientation;
            return UIInterfaceOrientationIsPortrait(orientation);
        } else {
            let orientation =
            UIApplication.sharedApplication.statusBarOrientation;
            return UIInterfaceOrientationIsPortrait(orientation);
        }
    }
    

    If there is for some reason no rootViewController yet fail safe to statusBarOrientation...

    0 讨论(0)
  • 2020-11-30 02:55

    Here is some Swift variables to make detection easier:

    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
    
    0 讨论(0)
  • 2020-11-30 03:01

    Wasn't satisfied by "UIDeviceOrientation" because when a UIViewcontroller orientation is fixed to a specific orientation you don't get a pertinent information with the device orientation, so the right thing to do is using "UIInterfaceOrientation".

    You can get the orientation from the UIViewController with a "self.interfaceOrientation", but when you are factorizing our code, you might need to do this kind of test outside a view controller, (custom view, a category…), so you still can access the information anywhere outside the controller by using the rootviewController:

    if (UIInterfaceOrientationIsLandscape(view.window.rootViewController.interfaceOrientation)) {
    }
    
    0 讨论(0)
提交回复
热议问题