ios横竖屏转换相关<全屏播放界面处理>

来源:互联网 发布:龙之信条优化设置 编辑:程序博客网 时间:2024/05/29 10:12

一直想写一篇关于屏幕旋转的文章,最近抽出些时间整理了下之前项目用到的屏幕旋转相关问题。
1.单个控制器开启转屏
2.自定义播放器横屏时全屏,连接投影仪时手机横屏,显示也为横屏

单个控制器开启转屏 需配置以下几点

在AppDelegate配置转屏属性

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // 横竖屏设置    NSUserDefaults *de = [NSUserDefaults standardUserDefaults];    [de setBool:NO forKey:@"SOrientations"];    return YES;}
// MARK: - 横竖屏设置-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{    NSUserDefaults *de = [NSUserDefaults standardUserDefaults];    bool a = [de boolForKey:@"SOrientations"];    if (a == YES) {        return UIInterfaceOrientationMaskAllButUpsideDown;    }    return UIInterfaceOrientationMaskPortrait;}

在需要强制转屏的控制器初始化时,配置转屏

 override func viewWillAppear(_ animated: Bool) {        super.viewWillAppear(animated)        let de = UserDefaults.standard        de.set(true, forKey: "SOrientations")   }

在需要强制转屏的控制器消失时,取消转屏配置

  override func viewWillDisappear(_ animated: Bool) {        super.viewWillDisappear(animated)        let de = UserDefaults.standard        de.set(false, forKey: "SOrientations")    }

到此,单个控制器允许转屏已经完成

如果需要在转屏时做一些操作,比如横屏状态下视频全屏播放自定义播放器在连接投影仪,手机横屏时投影仪也显示横屏

上面的代码仍然会用到
另外需要,
在viewWillAppear中添加代码

 let oritation = UIDevice.current.orientation         if(oritation == UIDeviceOrientation.landscapeLeft){    Rotate_Angel = Double.pi / 2   } UIDevice.current.beginGeneratingDeviceOrientationNotifications()        NotificationCenter.default.addObserver(self, selector: #selector(self.orientationChanged(_:)), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)

在viewWillDisappear中添加代码

 NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)

orientationChanged中配置转屏时播放器操作

 var Rotate_Angel: Double = Double.pi / 2 var lastOritation = UIDeviceOrientation.portrait func orientationChanged(_ notification: Foundation.Notification) {        let oritation = UIDevice.current.orientation        print("orientationChanged:", oritation.rawValue)        if(lastOritation == UIDeviceOrientation.portrait){            if(oritation == UIDeviceOrientation.landscapeLeft){                Rotate_Angel = Double.pi / 2            }else if(oritation == UIDeviceOrientation.landscapeRight){                Rotate_Angel = -Double.pi / 2            }        }        if(oritation == UIDeviceOrientation.landscapeLeft || oritation == UIDeviceOrientation.landscapeRight){            if(!isFullScreen){                //进入全屏设置                lastOritation = oritation            }        }else if(oritation == UIDeviceOrientation.portrait){            if(isFullScreen){                //返回竖屏设置                lastOritation = oritation            }        }    }

整个处理过程都在这里了,供大家参考

原创粉丝点击