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(){
}
}
网友评论