IOS - 判断手机屏幕旋转

来源:互联网 发布:淘宝图片没有美图秀秀 编辑:程序博客网 时间:2024/04/28 23:08

1.首先要监听屏幕旋转

NotificationCenter.default.addObserver(self, selector: #selector(self.printOrientation(noti:)), name:NSNotification.Name.UIApplicationDidChangeStatusBarOrientation, object: nil)

2.在监听方法里判断是什么状态

switch UIDevice.current.orientation {        case UIDeviceOrientation.portrait:            label?.text = "home 下"            // 正常手持状态            break        case UIDeviceOrientation.portraitUpsideDown:            label?.text = "home 上"            // 倒着            break        case UIDeviceOrientation.landscapeLeft:            label?.text = "home 右"            break        case UIDeviceOrientation.landscapeRight:            label?.text = "home 左"            break        case UIDeviceOrientation.faceUp:            label?.text = "home 面朝上"            break        case UIDeviceOrientation.faceDown:            label?.text = "home 面朝下"            break        default:            break        }

注释:
portrait:正常玩手机手的状态,home键在下面
portraitUpsideDown:home键在下面 (我旋转了,但是没有效果,不会打印这条,可能是pad有?)
landscapeLeft、landscapeRight:home右左,left对应home在右边,视频播放软件一般用这俩,尤其是用landscapeLeft,手机屏幕自动旋转功能开启旋转后自动全屏播放
faceUp、faceDown:手机面上下

0 0
原创粉丝点击