Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The whole point of this experiment is to test various transition techniques.
- import UIKit
- enum PickWhichExperimentToRun {
- case regular // call UIViewController.transition
- case brutal // no animation
- case animated // simple UIView.animate
- case transition // not UIViewController.transition but UIView's one
- }
- var yourChoice: PickWhichExperimentToRun = .animated
- @UIApplicationMain
- class AppDelegate: UIResponder, UIApplicationDelegate {
- var window: UIWindow?
- func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
- window = UIWindow(frame: UIScreen.main.bounds)
- window?.rootViewController = Experiment()
- window?.rootViewController?.title = "parent"
- window?.makeKeyAndVisible()
- return true
- }
- }
- class ViewController: UIViewController {
- func log(_ message: String = #function, comment: String? = nil) {
- print("\(type(of: self))[\(title ?? "")].\(message)\( comment == nil ? "" : " :: \(comment!)")")
- }
- override func didMove(toParentViewController parent: UIViewController?) {
- super.didMove(toParentViewController: parent)
- log()
- }
- override func willMove(toParentViewController parent: UIViewController?) {
- super.willMove(toParentViewController: parent)
- log()
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- log()
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- log()
- }
- override func viewDidAppear(_ animated: Bool) {
- super.viewDidAppear(animated)
- log()
- }
- override func viewWillDisappear(_ animated: Bool) {
- super.viewWillDisappear(animated)
- log()
- }
- override func viewDidDisappear(_ animated: Bool) {
- super.viewDidDisappear(animated)
- log()
- }
- }
- class Experiment : ViewController {
- let vc1 = ViewController()
- let vc2 = ViewController()
- var current: UIViewController? = nil
- override func viewDidLoad() {
- super.viewDidLoad()
- vc1.title = "vc1"
- vc2.title = "vc2"
- vc1.view.backgroundColor = .red
- vc2.view.backgroundColor = .blue
- addChildViewController(vc1)
- addChildViewController(vc2)
- view.addSubview(vc1.view)
- current = vc1
- // on tap, trigger transition
- let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(onTap(sender:)))
- view.addGestureRecognizer(tapRecognizer)
- }
- @objc func onTap(sender: UITapGestureRecognizer) {
- log(comment: "\(yourChoice)")
- guard let current = current else {
- // ignore the tap
- return
- }
- let nextVC: ViewController
- if current === vc1 {
- nextVC = vc2
- } else {
- nextVC = vc1
- }
- runExperiment(from:current, to:nextVC)
- self.current = nextVC
- }
- func runExperiment(from current: UIViewController, to nextVC: UIViewController) {
- switch yourChoice {
- case .regular:
- transition(from: current, to: nextVC, duration: 0.8, options: .transitionCrossDissolve, animations: nil)
- case .brutal:
- current.view.removeFromSuperview()
- view.addSubview(nextVC.view)
- case .animated:
- let inFrame = view.bounds
- let outLeftFrame = view.bounds.offsetBy(dx: -view.bounds.width, dy: 0)
- let outRightFrame = view.bounds.offsetBy(dx: view.bounds.width, dy: 0)
- nextVC.view.frame = outLeftFrame
- view.addSubview(nextVC.view)
- UIView.animate(withDuration: 0.8, animations: {
- current.view.frame = outRightFrame
- nextVC.view.frame = inFrame
- }, completion: {_ in
- current.view.removeFromSuperview()
- })
- case .transition:
- UIView.transition(from: current.view, to: nextVC.view, duration: 0.8, options: .transitionCrossDissolve, completion: nil)
- }
- }
- }
Add Comment
Please, Sign In to add comment