-
Notifications
You must be signed in to change notification settings - Fork 2
/
ViewController.swift
302 lines (269 loc) · 11.3 KB
/
ViewController.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//
// ViewController.swift
// AnimationPlanner-Sample
//
// Created by Pim on 02/06/2022.
//
import UIKit
import AnimationPlanner
class ViewController: UIViewController {
lazy var subview: UIView = {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
view.layer.cornerCurve = .continuous
return view
}()
lazy var stopButton = newStopButton()
lazy var resetButton = newResetButton()
// Sequence currently performing animations
var runningSequence: RunningSequence?
let testStopping: Bool = true // Set to true to display buttons to stop and reset animations
let performComplexAnimation: Bool = false // Set to true to run a more complex animation
func performAnimations() {
resetButton.isEnabled = false
if performComplexAnimation {
runComplexBuilderAnimation()
} else {
runSimpleBuilderAnimation()
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
performAnimations()
}
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(subview)
guard testStopping else {
return
}
view.addSubview(stopButton)
view.addSubview(resetButton)
stopButton.translatesAutoresizingMaskIntoConstraints = false
resetButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
stopButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
stopButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
stopButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor, constant: -8),
resetButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor, constant: 8),
resetButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
resetButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
])
}
}
extension ViewController {
func runSimpleBuilderAnimation() {
let view = setInitialSubviewState()
runningSequence = AnimationPlanner.plan {
Wait(0.35) // A delay waits for the given amount of seconds to start the next step
Animate(duration: 0.32, timingFunction: .quintOut) {
view.alpha = 1
view.center.y = self.view.bounds.midY
}
Wait(0.2)
Animate(duration: 0.32) {
view.transform = CGAffineTransform(scaleX: 2, y: 2)
view.layer.cornerRadius = 40
view.backgroundColor = .systemRed
}.timingFunction(.quintOut)
Wait(0.2)
AnimateSpring(duration: 0.25, dampingRatio: 0.52) {
view.backgroundColor = .systemBlue
view.layer.cornerRadius = 0
view.transform = .identity
}
Wait(0.58)
Animate(duration: 0.2) {
view.alpha = 0
view.transform = .identity
view.frame.origin.y = self.view.bounds.maxY
}.timingFunction(.circIn)
}.onComplete { finished in
if finished {
// Just to keep the flow going, let‘s run the animation again
self.runSimpleBuilderAnimation()
}
}
}
func runComplexBuilderAnimation() {
var sneakyCopy: UIView! // Don‘t worry, you‘ll see later
runningSequence = AnimationPlanner.plan {
let quarterHeight = view.bounds.height / 4
let view = setInitialSubviewState()
Wait(0.2)
Animate(duration: 1) {
view.alpha = 1
view.center.y = quarterHeight
}.timingFunction(.quartOut)
Wait(0.2)
Animate(duration: 0.35) {
view.transform = view.transform.scaledBy(x: 0.9, y: 0.9)
view.layer.cornerRadius = 40
}.timingFunction(.backOut)
Animate(duration: 1) {
view.frame.origin.y += quarterHeight
view.transform = .identity
}.timingFunction(.cubicInOut)
Wait(0.32)
var initialCornerRadius: CGFloat = 0
Extra {
// Trick to get specific value at time of animation
initialCornerRadius = view.layer.cornerRadius
}
let loopCount = 4
// Adding multiple steps can be done with a for-in statement
// or by adding `.mapSequence { }` or `.mapGroup { }` to any sequence
for index in 1...loopCount {
let offset = CGFloat(index) / CGFloat(loopCount)
let reversed = 1 - offset
Animate(duration: 0.32) {
view.transform = CGAffineTransform(
rotationAngle: .pi * offset
).scaledBy(
x: 1 + offset / 2,
y: 1 + offset / 2)
view.layer.cornerRadius = initialCornerRadius * reversed
}.spring(damping: 0.62)
Wait(0.2)
}
Extra {
// reset rotation
view.transform = view.transform.rotated(by: .pi)
}
// Example of using a custom method (defined further down) for a specific animation
addShakeSequence(shaking: view)
Extra {
// An ‘extra’ step performs non-animating setup logic
// like adding another view to the mix
sneakyCopy = view.sneakyCopy()
}
Animate(duration: 0.25) {
sneakyCopy.isHidden = false
sneakyCopy.transform = CGAffineTransform(translationX: 0, y: -view.frame.height - 20)
sneakyCopy.backgroundColor = .systemYellow
}.timingFunction(.backOut)
Wait(0.35)
Animate(duration: 1.2) {
view.transform = .identity
let offset = view.frame.origin.y + quarterHeight
view.frame.origin = CGPoint(x: view.frame.minX - (view.frame.width / 2) - 10, y: offset)
sneakyCopy.frame = view.frame.offsetBy(dx: view.frame.width + 20, dy: 0)
view.backgroundColor = .systemPink
}.timingFunction(.quartInOut)
Wait(0.5)
Group {
// A group performs all of its animations at once,
// finishing when the longest animation completes
// Use a delay for a staggered effect
AnimateDelayed(delay: 0.2, duration: 0.5) {
sneakyCopy.transform = CGAffineTransform(translationX: 0, y: -50).concatenating(sneakyCopy.transform)
}.timingFunction(.backOut)
AnimateDelayed(delay: 0.1, duration: 0.2) {
view.layer.borderColor = view.backgroundColor?.cgColor
view.layer.borderWidth = 4
sneakyCopy.layer.borderColor = sneakyCopy.backgroundColor?.cgColor
sneakyCopy.layer.borderWidth = 4
}.timingFunction(.cubicOut)
Animate(duration: 1) {
let viewColor = view.backgroundColor
view.backgroundColor = sneakyCopy.backgroundColor
sneakyCopy.backgroundColor = viewColor
}
}
Wait(0.32)
Animate(duration: 0.5) {
view.alpha = 0
sneakyCopy?.alpha = 0
// you can use values set in previous animations
// as the animations are created after the previous animation completes
view.transform = view.transform.translatedBy(x: 0, y: quarterHeight)
sneakyCopy?.transform = view.transform.translatedBy(x: 0, y: quarterHeight)
}
}.onComplete { finished in
if finished {
sneakyCopy.removeFromSuperview()
self.runComplexBuilderAnimation()
}
}
}
}
extension ViewController {
func setInitialSubviewState() -> UIView {
subview.alpha = 0
subview.transform = .identity
subview.frame.size = CGSize(width: 100, height: 100)
subview.center.x = view.bounds.midX
subview.frame.origin.y = view.bounds.minY
subview.backgroundColor = .systemOrange
subview.layer.cornerRadius = 16
subview.layer.borderWidth = 0
subview.layer.borderColor = nil
return subview
}
/// Adds a custom shake animation sequence on the provided view
/// - Parameter view: View to which the transform should be applied
/// - Returns: Animations to be added to the sequence
@SequenceBuilder
func addShakeSequence(shaking view: UIView) -> [SequenceAnimatable] {
var baseTransform: CGAffineTransform = .identity
let count = 50
let maxRadius: CGFloat = 4
let values = (0..<count).map { CGFloat($0) / CGFloat(count) }.map { $0 * maxRadius }
Extra { baseTransform = view.transform }
for radius in values {
Animate(duration: 0.015) {
view.transform = baseTransform
.translatedBy(
x:CGFloat.random(in: -radius...radius),
y: CGFloat.random(in: -radius...radius)
)
}.timingFunction(.quintOut)
}
}
}
private extension ViewController {
func newStopButton() -> UIButton {
var configuration = UIButton.Configuration.filled()
configuration.buttonSize = .large
configuration.baseBackgroundColor = .systemRed
configuration.cornerStyle = .large
configuration.title = "Stop"
return UIButton(configuration: configuration, primaryAction: UIAction { [unowned self] _ in
runningSequence?.stopAnimations()
resetButton.isEnabled = true
})
}
func newResetButton() -> UIButton {
var configuration = UIButton.Configuration.filled()
configuration.buttonSize = .large
configuration.baseBackgroundColor = .systemGreen
configuration.cornerStyle = .large
configuration.title = "Reset"
let button = UIButton(configuration: configuration, primaryAction: UIAction { [unowned self] _ in
performAnimations()
})
button.isEnabled = false
return button
}
}
extension UIView {
func sneakyCopy() -> Self? {
// 🫣🫣🫣
do {
let archiver = NSKeyedArchiver(requiringSecureCoding: false)
archiver.encodeRootObject(self)
let data = archiver.encodedData
let unarchiver = try NSKeyedUnarchiver(forReadingFrom: data)
unarchiver.requiresSecureCoding = false
guard let view = unarchiver.decodeObject() as? Self else {
return nil
}
view.isHidden = true
superview?.insertSubview(view, belowSubview: self)
return view
}
catch {
print(error)
return nil
}
}
}