override func viewDidLoad() {
super.viewDidLoad()
//回転感知Observer
NotificationCenter.default.addObserver(self,
selector: #selector(self.rotationChange(notification:)),
name:UIDevice.orientationDidChangeNotification,
object: nil)
}
@objc func rotationChange(notification: NSNotification){
let device = UIDevice.current
if device.orientation.isLandscape {
print("横画面になりました")
} else {
print("タテ画面になりました")
}
}
こういう書き方だと完全ではありません。
@objc func rotationChange(notification: NSNotification){
let device = UIDevice.current
if device.orientation.isLandscape {
print("横画面になりました")
} else if device.orientation.isFlat{
print("フラットになりました")
}else if device.orientation.isPortrait{
print("タテ画面になりました")
}
}
デバイスの向きには横・縦の他に平があるようです
こうすることで精度が先ほどのコードよりは精度が増すと思います。
というかこっちの書き方の方がおすすめかも