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("屏幕朝下")
}
}