ios屏幕旋转

2017-05-09  本文已影响0人  AntKing

在View中监听屏幕的旋转

- (void)layoutSubviews

{

     [self _shouldRotateToOrientation:(UIDeviceOrientation)[UIApplication sharedApplication].statusBarOrientation];//这里推荐使用状态栏判断

}

-(void)_shouldRotateToOrientation:(UIDeviceOrientation)orientation {
   if (orientation == UIDeviceOrientationPortrait ||orientation ==
                UIDeviceOrientationPortraitUpsideDown) { // 竖屏

    } else { // 横屏

    }
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];

- (void)orientChange:(NSNotification *)notification{
UIInterfaceOrientation interfaceOritation = [[UIApplication sharedApplication] statusBarOrientation];

/*
statusBarOrientation   4种状态:
UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft

*/
}

Snip20170916_4.png
class AppDelegate: UIApplicationDelegate{

//添加一个控制屏幕旋转的属性
var allowRotation : Bool = false

    ///横竖屏解决方案
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        if allowRotation == true {
            return .all
        }else{
            return .portrait
        }
    }
}


//在其他要旋转的控制器中只需要设置allowRotation的值就可以了

        // 用appdelegate来控制设备的旋转
        let appdelegate = UIApplication.shared.delegate as! AppDelegate
        appdelegate.allowRotation = true


    // MARK:-----屏幕旋转---------------
    override var shouldAutorotate: Bool {
        return true
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        if player.isLocked {
            return .landscape
        } else {
            return .all
        }
    }



上一篇下一篇

猜你喜欢

热点阅读