横竖屏监测
func addOrientationChangedNotification() {
//感知设备方向 - 开启监听设备方向
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
//添加通知,监听设备方向改变
NotificationCenter.default.addObserver(self, selector: #selector(receivedRotation),
name: UIDevice.orientationDidChangeNotification, object: nil)
//关闭设备监听
//UIDevice.current.endGeneratingDeviceOrientationNotifications()
}
//通知监听触发的方法
@objc func receivedRotation(){
let device = UIDevice.current
print("*******",device.orientation.rawValue)
switch device.orientation{
case .landscapeLeft, .landscapeRight:
if isBack {
backView.isLandScape = true
} else {
frontView.isLandScape = true
}
default:
if isBack {
backView.isLandScape = false
} else {
frontView.isLandScape = false
}
}
}
deinit {
// 移除通知
NotificationCenter.default.removeObserver(self)
UIDevice.current.endGeneratingDeviceOrientationNotifications()
}