jr0*_*00n 7 uiviewcontroller ios
我将我的iPad应用程序移植到iOS8和Swift.
在肖像中,我使用根UIViewController,当de设备旋转到横向时,我转向另一个UIViewController.我提出了两个解决方案,一个基于UIDevice通知,另一个基于willRotateToInterfaceRotation.我总是试图远离观察者模式,这只是我的习惯.
Observer工作正常,但是func的UIViewController中的覆盖willRotateToInterfaceOrientation(toInterfaceOrientation:UIInterfaceOrientation,duration:NSTimeInterval)看起来更清晰;)
但现在在iOS8中该功能已被弃用,我应该使用
func viewWillTransitionToSize(_ size: CGSize,
withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
Run Code Online (Sandbox Code Playgroud)
但我不知道如何使用它来获得相同的结果.
这是rootViewController:
override func willRotateToInterfaceOrientation(
toInterfaceOrientation: UIInterfaceOrientation,
duration: NSTimeInterval) {
if (toInterfaceOrientation == UIInterfaceOrientation.LandscapeLeft ||
toInterfaceOrientation == UIInterfaceOrientation.LandscapeRight) {
self.performSegueWithIdentifier("toLandscape", sender: self)
}
}
Run Code Online (Sandbox Code Playgroud)
和UIViewControllerLanscape:
override func willRotateToInterfaceOrientation(
toInterfaceOrientation: UIInterfaceOrientation,
duration: NSTimeInterval) {
if (toInterfaceOrientation == UIInterfaceOrientation.Portrait ||
toInterfaceOrientation == UIInterfaceOrientation.PortraitUpsideDown) {
self.presentingViewController?.dismissViewControllerAnimated(true,
completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
我不喜欢使用弃用的函数,所以我不知道该怎么办...去观察者或者什么?
这是我使用的代码(仅)根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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我非常喜欢您对如何为这些已弃用的功能提供解决方案的想法.对我来说这完全是个谜......或者观察者现在可能是更好的解决方案.请分享您的想法.
问候,
jr00n