我将我的iPad应用移植到iOS8和Swift。
在肖像中,我使用根UIViewController,当de设备旋转到横向时,我转向另一个UIViewController。我提出了两个解决方案,一个基于UIDevice通知,另一个基于willRotateToInterfaceRotation
。我总是试图远离观察者模式,这只是我的习惯。
Observer工作正常,但在UIViewController中都覆盖了 func willRotateToInterfaceOrientation(toInterfaceOrientation:UIInterfaceOrientation,duration:NSTimeInterval) 我的眼睛看起来更干净;)
但现在在iOS8中该功能已被弃用,我应该使用
func viewWillTransitionToSize(_ size: CGSize,
withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
但我不知道如何使用它来获得相同的结果。
这是rootViewController
:
override func willRotateToInterfaceOrientation(
toInterfaceOrientation: UIInterfaceOrientation,
duration: NSTimeInterval) {
if (toInterfaceOrientation == UIInterfaceOrientation.LandscapeLeft ||
toInterfaceOrientation == UIInterfaceOrientation.LandscapeRight) {
self.performSegueWithIdentifier("toLandscape", sender: self)
}
}
和UIViewControllerLanscape
:
override func willRotateToInterfaceOrientation(
toInterfaceOrientation: UIInterfaceOrientation,
duration: NSTimeInterval) {
if (toInterfaceOrientation == UIInterfaceOrientation.Portrait ||
toInterfaceOrientation == UIInterfaceOrientation.PortraitUpsideDown) {
self.presentingViewController?.dismissViewControllerAnimated(true,
completion: nil)
}
}
我不想使用已弃用的功能,所以我怀疑该怎么做...去观察者或者什么?
这是我使用的代码(仅)根UIViewController是UIDeviceOrientationDidChangeNotification的观察者:
override func awakeFromNib() {
let dev = UIDevice.currentDevice()
dev.beginGeneratingDeviceOrientationNotifications()
let nc = NSNotificationCenter.defaultCenter()
nc.addObserver(self, selector: "orientationChanged:", name: UIDeviceOrientationDidChangeNotification, object: dev)
}
func orientationChanged(note: NSNotification) -> () {
if let uidevice: UIDevice = note.object? as? UIDevice {
switch uidevice.orientation {
case .LandscapeLeft, .LandscapeRight:
self.performSegueWithIdentifier("toLandscape", sender: self)
default:
self.dismissViewControllerAnimated(true, completion: nil)
}
}
}
我非常喜欢您对如何为这些已弃用的功能提供解决方案的想法。这对我来说完全是个谜......或者观察者现在可能是更好的解决方案。请分享您的想法。
此致
jr00n