Detecting iOS UIDevice orientation

后端 未结 8 890
一生所求
一生所求 2020-11-28 03:29

I need to detect when the device is in portrait orientation so that I can fire off a special animation. But I do not want my view to autorotate.

How do I override a

相关标签:
8条回答
  • 2020-11-28 04:19

    If you do not want to create device object, you can also use

    -(void) seObserverForOrientationChanging
    {
        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        [[NSNotificationCenter defaultCenter]
         addObserver:self selector:@selector(orientationChanged:)
         name:UIDeviceOrientationDidChangeNotification
         object:[UIDevice currentDevice]];
    }
    
    
    - (void) orientationChanged:(NSNotification *)note
    {
        if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)){
            //Do something in landscape
        }
        else {
            //Do something in portrait
        }
    }
    
    0 讨论(0)
  • 2020-11-28 04:22

    In Swift 5.0.

    DeviceOrientation vs. ScreenSize vs StatusBar.isLandscape?

    If you want to detect changes of orientation in background, and also need to detect face up/down changes, check this link In Swift, how to get the device orientation correctly right after it's launched?

    It covers how to correctly detect the orientation in Swift using 3 alternate ways to do it: - viewWillTransitionToSize() - Using Observer - Using the status bar

    0 讨论(0)
提交回复
热议问题