手動でUIを回転させる
override var shouldAutorotate: Bool {
return false
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(QRCodeReaderViewController.onOrientationChange(_:)), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}
func onOrientationChange(_ notification: Notification){
let deviceOrientation = UIDevice.current.orientation
switch deviceOrientation {
case .portrait:
angle = 0
case .portraitUpsideDown:
angle = CGFloat(M_PI)
case .landscapeLeft:
angle = CGFloat(M_PI_2)
case .landscapeRight:
angle = CGFloat(-M_PI_2)
default:
return
}
UIView.animate(withDuration: 0.2, animations: {
self.view.transform = CGAffineTransform(rotationAngle: angle)
self.view.frame = CGRect(x, y, width, height)
})
}