Guest User

Untitled

a guest
Nov 19th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. // The whole point of this experiment is to test various transition techniques.
  2.  
  3. import UIKit
  4.  
  5. enum PickWhichExperimentToRun {
  6. case regular // call UIViewController.transition
  7. case brutal // no animation
  8. case animated // simple UIView.animate
  9. case transition // not UIViewController.transition but UIView's one
  10. }
  11.  
  12. var yourChoice: PickWhichExperimentToRun = .animated
  13.  
  14. @UIApplicationMain
  15. class AppDelegate: UIResponder, UIApplicationDelegate {
  16. var window: UIWindow?
  17.  
  18. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  19.  
  20. window = UIWindow(frame: UIScreen.main.bounds)
  21.  
  22. window?.rootViewController = Experiment()
  23. window?.rootViewController?.title = "parent"
  24.  
  25. window?.makeKeyAndVisible()
  26.  
  27. return true
  28. }
  29. }
  30.  
  31. class ViewController: UIViewController {
  32.  
  33. func log(_ message: String = #function, comment: String? = nil) {
  34. print("\(type(of: self))[\(title ?? "")].\(message)\( comment == nil ? "" : " :: \(comment!)")")
  35. }
  36.  
  37. override func didMove(toParentViewController parent: UIViewController?) {
  38. super.didMove(toParentViewController: parent)
  39. log()
  40. }
  41.  
  42. override func willMove(toParentViewController parent: UIViewController?) {
  43. super.willMove(toParentViewController: parent)
  44. log()
  45. }
  46.  
  47. override func viewDidLoad() {
  48. super.viewDidLoad()
  49. log()
  50. }
  51.  
  52. override func viewWillAppear(_ animated: Bool) {
  53. super.viewWillAppear(animated)
  54. log()
  55. }
  56.  
  57. override func viewDidAppear(_ animated: Bool) {
  58. super.viewDidAppear(animated)
  59. log()
  60. }
  61.  
  62. override func viewWillDisappear(_ animated: Bool) {
  63. super.viewWillDisappear(animated)
  64. log()
  65. }
  66.  
  67. override func viewDidDisappear(_ animated: Bool) {
  68. super.viewDidDisappear(animated)
  69. log()
  70. }
  71. }
  72.  
  73. class Experiment : ViewController {
  74.  
  75. let vc1 = ViewController()
  76. let vc2 = ViewController()
  77.  
  78. var current: UIViewController? = nil
  79.  
  80. override func viewDidLoad() {
  81. super.viewDidLoad()
  82.  
  83. vc1.title = "vc1"
  84. vc2.title = "vc2"
  85.  
  86. vc1.view.backgroundColor = .red
  87. vc2.view.backgroundColor = .blue
  88.  
  89. addChildViewController(vc1)
  90. addChildViewController(vc2)
  91.  
  92. view.addSubview(vc1.view)
  93. current = vc1
  94.  
  95. // on tap, trigger transition
  96. let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(onTap(sender:)))
  97. view.addGestureRecognizer(tapRecognizer)
  98. }
  99.  
  100. @objc func onTap(sender: UITapGestureRecognizer) {
  101. log(comment: "\(yourChoice)")
  102.  
  103. guard let current = current else {
  104. // ignore the tap
  105. return
  106. }
  107.  
  108. let nextVC: ViewController
  109. if current === vc1 {
  110. nextVC = vc2
  111. } else {
  112. nextVC = vc1
  113. }
  114.  
  115. runExperiment(from:current, to:nextVC)
  116. self.current = nextVC
  117. }
  118.  
  119. func runExperiment(from current: UIViewController, to nextVC: UIViewController) {
  120. switch yourChoice {
  121. case .regular:
  122. transition(from: current, to: nextVC, duration: 0.8, options: .transitionCrossDissolve, animations: nil)
  123. case .brutal:
  124. current.view.removeFromSuperview()
  125. view.addSubview(nextVC.view)
  126. case .animated:
  127. let inFrame = view.bounds
  128. let outLeftFrame = view.bounds.offsetBy(dx: -view.bounds.width, dy: 0)
  129. let outRightFrame = view.bounds.offsetBy(dx: view.bounds.width, dy: 0)
  130.  
  131. nextVC.view.frame = outLeftFrame
  132. view.addSubview(nextVC.view)
  133.  
  134. UIView.animate(withDuration: 0.8, animations: {
  135. current.view.frame = outRightFrame
  136. nextVC.view.frame = inFrame
  137. }, completion: {_ in
  138. current.view.removeFromSuperview()
  139. })
  140. case .transition:
  141. UIView.transition(from: current.view, to: nextVC.view, duration: 0.8, options: .transitionCrossDissolve, completion: nil)
  142. }
  143. }
  144.  
  145. }
Add Comment
Please, Sign In to add comment