iOS 监听手机屏幕旋转

监听屏幕旋转

首先所要监听的NSNotificationNameUIApplicationDidChangeStatusBarOrientationNotification而非UIDeviceOrientationDidChangeNotification,因为UIDeviceOrientationDidChangeNotification在应用进入后台applicationDidEnterBackground和进入前台applicationWillEnterForeground同样会响应通知,通常我们不需要程序在进入后台、前台进行屏幕旋转,这就可能会造成可能的界面错乱,所以正确的做法是:

  1. 添加通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChanged:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
    2.实现通知方法
    – (void)statusBarOrientationChanged:(NSNotification )noti {
    /

    UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown,
    UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
    */
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationLandscapeLeft) {//

    }
    if (orientation == UIInterfaceOrientationLandscapeRight) {

    }
    if (orientation == UIInterfaceOrientationPortrait) {

    }

}

你可能感兴趣的:(iOS,手机屏幕旋转监听)