iOS Swift3 监听屏幕是否旋转了

1. 先增加屏幕监听通知

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        // 检测设备方向
         NotificationCenter.default.addObserver(self, selector: #selector(receivedRotation), name: .UIDeviceOrientationDidChange, object: nil)
    }

2. 通知函数

    //通知监听触发的方法
    func receivedRotation(){
        // 屏幕方向
        switch UIDevice.current.orientation {
        case UIDeviceOrientation.unknown:
            print("方向未知")
        case .portrait: // Device oriented vertically, home button on the bottom
            print("屏幕直立")
        case .portraitUpsideDown: // Device oriented vertically, home button on the top
            print("屏幕倒立")
        case .landscapeLeft: // Device oriented horizontally, home button on the right
            print("屏幕左在上方")
        case .landscapeRight: // Device oriented horizontally, home button on the left
            print("屏幕右在上方")
        case .faceUp: // Device oriented flat, face up
            print("屏幕朝上")
        case .faceDown: // Device oriented flat, face down
            print("屏幕朝下")
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,285评论 30 472
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,866评论 25 709
  • iOS面试小贴士 ———————————————回答好下面的足够了------------------------...
    不言不爱阅读 2,083评论 0 7
  • 第一次听说比特币是在2010年前后,如流星划过天空一样,只是给我留下了一点新奇的感觉,之后我的世界就再也没有它的影...
    边缘狼周红兵阅读 238评论 0 0
  • 有人说:一辈子那么长,遇到一个懂自己的人真的不容易,遇到一个知己也是很不容易。我觉得也是这样吧。懂你的人你一个眼神...
    乖摸摸头97阅读 153评论 0 1