-
Notifications
You must be signed in to change notification settings - Fork 44
/
Contents.swift
98 lines (66 loc) · 2.92 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import Foundation
import UIKit
import PlaygroundSupport
class ViewController: UIViewController {
// MARK: - Properties
let rect = CGRect(x: 170, y: 300, width: 50, height: 50)
lazy var customView = UIView(frame: rect)
lazy var nextButton: UIButton = {
let button = UIButton(frame: CGRect(x: 140, y: 500, width: 110, height: 44))
button.setTitle("Next Screen", for: .normal)
button.backgroundColor = .lightGray
button.addTarget(self, action: #selector(ViewController.handleNextViewController(_:)), for: .touchUpInside)
return button
}()
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
print("ViewController loaded: " , #function)
view.backgroundColor = .white
title = "Root"
// Custom view will initialized here, where the first time it was accessed
customView.backgroundColor = .red
view.addSubview(customView)
// Next button UIButton controll will be lazyly created here for the first time
view.addSubview(nextButton)
}
// MARK: - Actions
@objc func handleNextViewController(_ button: UIButton) {
let nextViewController = NextViewController()
navigationController?.pushViewController(nextViewController, animated: true)
}
}
class NextViewController: UIViewController {
// MARK: - Properties
lazy var label: UILabel = prepareMessage()
// MARK: - Private methods
private func prepareMessage() -> UILabel {
let label = UILabel(frame: CGRect(x: 100, y: 300, width: 250, height: 56))
label.text = "Hello Next View Controller!"
label.textColor = .black
label.alpha = 0.0
// This method will be called each time the view controller is pushed and the setup for UILabel will be delayed
print("NextViewController loaded: ", #function)
return label
}
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
print("NextViewController loaded: ", #function)
title = "Next View Controller"
view.backgroundColor = .white
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("NextViewController loaded: ", #function)
// The view controller's viewDidLoad is called first, then the above print will be outputed to the console, and finally UILabel with the message will be created and animated
view.addSubview(label)
UIView.animate(withDuration: 2.0) {
self.label.alpha = 1.0
}
}
}
let viewController = ViewController()
let navigation = UINavigationController(rootViewController: viewController)
PlaygroundPage.current.liveView = navigation
PlaygroundPage.current.needsIndefiniteExecution = true