美文网首页
iOS:监听手机方向

iOS:监听手机方向

作者: Liu____ | 来源:发表于2021-03-22 09:05 被阅读0次
    import UIKit
     
    class ViewController: UIViewController {
     
      
        override func viewDidLoad() {
            super.viewDidLoad()
          //各个方向监听
           NotificationCenter.default.addObserver(self, selector:     #selector(ViewController.orientationChanged(_:)), name: UIDevice.orientationDidChangeNotification, object:nil)
          //状态栏方向改变
           NotificationCenter.default.addObserver(self, selector: #selector(didChangeStatusBar), name: UIApplication.didChangeStatusBarOrientationNotification, object: nil)
           }
    
        @objc func orientationChanged(_ notification: Notification){
                    let device = UIDevice.current
                    switch device.orientation
                    {
                    case .portrait:
                        print("面向设备保持垂直,home键位于下部")
                    case .portraitUpsideDown:
                        print("面向设备保持垂直,home键位于上部")
                    case .landscapeLeft:
                        print("面向设备保持水平,home键位于右侧")
                    case .landscapeRight:
                        print("面向设备保持水平,home键位于左侧")
                    case .faceUp:
                            print("设备平放,home键朝上")
                    case .faceDown:
                            print("设备平放,home键朝下")
                    case .unknown:
                            print("方向未知")
                    }
            
            
             }
    
       @objc func didChangeStatusBar(){
                 
        
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:iOS:监听手机方向

          本文链接:https://www.haomeiwen.com/subject/wwaacltx.html