Skip to content

Instantly share code, notes, and snippets.

@antonyharfield
Last active November 2, 2017 10:41
Show Gist options
  • Save antonyharfield/dd536a2e4e50618900eccc5c923c8a59 to your computer and use it in GitHub Desktop.
Save antonyharfield/dd536a2e4e50618900eccc5c923c8a59 to your computer and use it in GitHub Desktop.
Swift category/extension for animating the switch/swap to a new root view controller at the UIWindow level
// Swift category/extension for animating between rootViewControllers at the UIWindow level
import UIKit
extension UIWindow {
func setRootViewController(newRootViewController: UIViewController, animated: Bool) {
// No animation required
if !animated || self.rootViewController == nil {
self.rootViewController = newRootViewController
return
}
// Snapshot current window and add it as a subview of the new root VC
let snapshotView = self.snapshotViewAfterScreenUpdates(true)
newRootViewController.view.addSubview(snapshotView)
// Switch to new root VC
self.rootViewController = newRootViewController
// Fade out out the snapshot
UIView.animateWithDuration(0.3, animations: {
snapshotView.layer.opacity = 0.0
snapshotView.layer.transform = CATransform3DMakeScale(1.5, 1.5, 1.5)
}) { (_) in
snapshotView.removeFromSuperview()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment