-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
DefaultAnimationPreprocessor.swift
392 lines (364 loc) · 15.7 KB
/
DefaultAnimationPreprocessor.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// The MIT License (MIT)
//
// Copyright (c) 2016 Luke Zhao <me@lkzhao.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#if canImport(UIKit)
import UIKit
public enum HeroDefaultAnimationType {
public enum Direction: HeroStringConvertible {
case left, right, up, down
public static func from(node: ExprNode) -> Direction? {
switch node.name {
case "left": return .left
case "right": return .right
case "up": return .up
case "down": return .down
case "leading": return .leading
case "trailing": return .trailing
default: return nil
}
}
public static var leadingToTrailing: CascadeDirection {
return !Locale.isDeviceLanguageRightToLeft ? .leftToRight : .rightToLeft
}
public static var trailingToLeading: CascadeDirection {
return !Locale.isDeviceLanguageRightToLeft ? .rightToLeft : .leftToRight
}
public static var leading: Direction {
return !Locale.isDeviceLanguageRightToLeft ? .left : .right
}
public static var trailing: Direction {
return !Locale.isDeviceLanguageRightToLeft ? .right : .left
}
}
public enum Strategy {
case forceLeftToRight, forceRightToLeft, userInterface
func defaultDirection(presenting: Bool) -> Direction {
switch self {
case .forceLeftToRight:
return presenting ? .left : .right
case .forceRightToLeft:
return presenting ? .right : .left
case .userInterface:
return presenting ? .leading : .trailing
}
}
}
case auto
case push(direction: Direction)
case pull(direction: Direction)
case cover(direction: Direction)
case uncover(direction: Direction)
case slide(direction: Direction)
case zoomSlide(direction: Direction)
case pageIn(direction: Direction)
case pageOut(direction: Direction)
case fade
case zoom
case zoomOut
indirect case selectBy(presenting: HeroDefaultAnimationType, dismissing: HeroDefaultAnimationType)
public static func autoReverse(presenting: HeroDefaultAnimationType) -> HeroDefaultAnimationType {
return .selectBy(presenting: presenting, dismissing: presenting.reversed())
}
case none
func reversed() -> HeroDefaultAnimationType {
switch self {
case .push(direction: .up):
return .pull(direction: .down)
case .push(direction: .right):
return .pull(direction: .left)
case .push(direction: .down):
return .pull(direction: .up)
case .push(direction: .left):
return .pull(direction: .right)
case .pull(direction: .up):
return .push(direction: .down)
case .pull(direction: .right):
return .push(direction: .left)
case .pull(direction: .down):
return .push(direction: .up)
case .pull(direction: .left):
return .push(direction: .right)
case .cover(direction: .up):
return .uncover(direction: .down)
case .cover(direction: .right):
return .uncover(direction: .left)
case .cover(direction: .down):
return .uncover(direction: .up)
case .cover(direction: .left):
return .uncover(direction: .right)
case .uncover(direction: .up):
return .cover(direction: .down)
case .uncover(direction: .right):
return .cover(direction: .left)
case .uncover(direction: .down):
return .cover(direction: .up)
case .uncover(direction: .left):
return .cover(direction: .right)
case .slide(direction: .up):
return .slide(direction: .down)
case .slide(direction: .down):
return .slide(direction: .up)
case .slide(direction: .left):
return .slide(direction: .right)
case .slide(direction: .right):
return .slide(direction: .left)
case .zoomSlide(direction: .up):
return .zoomSlide(direction: .down)
case .zoomSlide(direction: .down):
return .zoomSlide(direction: .up)
case .zoomSlide(direction: .left):
return .zoomSlide(direction: .right)
case .zoomSlide(direction: .right):
return .zoomSlide(direction: .left)
case .pageIn(direction: .up):
return .pageOut(direction: .down)
case .pageIn(direction: .right):
return .pageOut(direction: .left)
case .pageIn(direction: .down):
return .pageOut(direction: .up)
case .pageIn(direction: .left):
return .pageOut(direction: .right)
case .pageOut(direction: .up):
return .pageIn(direction: .down)
case .pageOut(direction: .right):
return .pageIn(direction: .left)
case .pageOut(direction: .down):
return .pageIn(direction: .up)
case .pageOut(direction: .left):
return .pageIn(direction: .right)
case .zoom:
return .zoomOut
case .zoomOut:
return .zoom
default:
return self
}
}
public var label: String? {
let mirror = Mirror(reflecting: self)
if let associated = mirror.children.first {
let valuesMirror = Mirror(reflecting: associated.value)
if !valuesMirror.children.isEmpty {
let parameters = valuesMirror.children.map { ".\($0.value)" }.joined(separator: ",")
return ".\(associated.label ?? "")(\(parameters))"
}
return ".\(associated.label ?? "")(.\(associated.value))"
}
return ".\(self)"
}
}
extension HeroDefaultAnimationType: HeroStringConvertible {
public static func from(node: ExprNode) -> HeroDefaultAnimationType? {
let name: String = node.name
let parameters: [ExprNode] = (node as? CallNode)?.arguments ?? []
switch name {
case "auto":
return .auto
case "push":
if let node = parameters.get(0), let direction = Direction.from(node: node) {
return .push(direction: direction)
}
case "pull":
if let node = parameters.get(0), let direction = Direction.from(node: node) {
return .pull(direction: direction)
}
case "cover":
if let node = parameters.get(0), let direction = Direction.from(node: node) {
return .cover(direction: direction)
}
case "uncover":
if let node = parameters.get(0), let direction = Direction.from(node: node) {
return .uncover(direction: direction)
}
case "slide":
if let node = parameters.get(0), let direction = Direction.from(node: node) {
return .slide(direction: direction)
}
case "zoomSlide":
if let node = parameters.get(0), let direction = Direction.from(node: node) {
return .zoomSlide(direction: direction)
}
case "pageIn":
if let node = parameters.get(0), let direction = Direction.from(node: node) {
return .pageIn(direction: direction)
}
case "pageOut":
if let node = parameters.get(0), let direction = Direction.from(node: node) {
return .pageOut(direction: direction)
}
case "fade": return .fade
case "zoom": return .zoom
case "zoomOut": return .zoomOut
case "selectBy":
if let presentingNode = parameters.get(0),
let presenting = HeroDefaultAnimationType.from(node: presentingNode),
let dismissingNode = parameters.get(1),
let dismissing = HeroDefaultAnimationType.from(node: dismissingNode) {
return .selectBy(presenting: presenting, dismissing: dismissing)
}
case "none": return HeroDefaultAnimationType.none
default: break
}
return nil
}
}
class DefaultAnimationPreprocessor: BasePreprocessor {
func shift(direction: HeroDefaultAnimationType.Direction, appearing: Bool, size: CGSize? = nil, transpose: Bool = false) -> CGPoint {
let size = size ?? context.container.bounds.size
let rtn: CGPoint
switch direction {
case .left, .right:
rtn = CGPoint(x: (direction == .right) == appearing ? -size.width : size.width, y: 0)
case .up, .down:
rtn = CGPoint(x: 0, y: (direction == .down) == appearing ? -size.height : size.height)
}
if transpose {
return CGPoint(x: rtn.y, y: rtn.x)
}
return rtn
}
override func process(fromViews: [UIView], toViews: [UIView]) {
guard let hero = hero, let toView = hero.toView, let fromView = hero.fromView else { return }
var defaultAnimation = hero.defaultAnimation
let inNavigationController = hero.inNavigationController
let inTabBarController = hero.inTabBarController
let toViewController = hero.toViewController
let fromViewController = hero.fromViewController
let presenting = hero.isPresenting
let fromOverFullScreen = hero.fromOverFullScreen
let toOverFullScreen = hero.toOverFullScreen
let animators = hero.animators
if case .auto = defaultAnimation {
if inNavigationController, let navAnim = toViewController?.navigationController?.hero.navigationAnimationType {
defaultAnimation = navAnim
} else if inTabBarController, let tabAnim = toViewController?.tabBarController?.hero.tabBarAnimationType {
defaultAnimation = tabAnim
} else if let modalAnim = (presenting ? toViewController : fromViewController)?.hero.modalAnimationType {
defaultAnimation = modalAnim
}
}
if case .selectBy(let presentAnim, let dismissAnim) = defaultAnimation {
defaultAnimation = presenting ? presentAnim : dismissAnim
}
if case .auto = defaultAnimation {
if animators.contains(where: { $0.canAnimate(view: toView, appearing: true) || $0.canAnimate(view: fromView, appearing: false) }) {
defaultAnimation = .none
} else if inNavigationController {
let direction = hero.defaultAnimationDirectionStrategy.defaultDirection(presenting: presenting)
defaultAnimation = .push(direction: direction)
} else if inTabBarController {
let direction = hero.defaultAnimationDirectionStrategy.defaultDirection(presenting: presenting)
defaultAnimation = .slide(direction: direction)
} else {
defaultAnimation = .fade
}
}
if case .none = defaultAnimation {
return
}
context[fromView] = [.timingFunction(.standard), .duration(0.35)]
context[toView] = [.timingFunction(.standard), .duration(0.35)]
let shadowState: [HeroModifier] = [.shadowOpacity(0.5),
.shadowColor(.black),
.shadowRadius(5),
.shadowOffset(.zero),
.masksToBounds(false)]
switch defaultAnimation {
case .push(let direction):
context.insertToViewFirst = false
context[toView]!.append(contentsOf: [.translate(shift(direction: direction, appearing: true)),
.shadowOpacity(0),
.beginWith(modifiers: shadowState),
.timingFunction(.deceleration)])
context[fromView]!.append(contentsOf: [.translate(shift(direction: direction, appearing: false) / 3),
.overlay(color: .black, opacity: 0.1),
.timingFunction(.deceleration)])
case .pull(let direction):
context.insertToViewFirst = true
context[fromView]!.append(contentsOf: [.translate(shift(direction: direction, appearing: false)),
.shadowOpacity(0),
.beginWith(modifiers: shadowState)])
context[toView]!.append(contentsOf: [.translate(shift(direction: direction, appearing: true) / 3),
.overlay(color: .black, opacity: 0.1)])
case .slide(let direction):
context[fromView]!.append(contentsOf: [.translate(shift(direction: direction, appearing: false))])
context[toView]!.append(contentsOf: [.translate(shift(direction: direction, appearing: true))])
case .zoomSlide(let direction):
context[fromView]!.append(contentsOf: [.translate(shift(direction: direction, appearing: false)), .scale(0.8)])
context[toView]!.append(contentsOf: [.translate(shift(direction: direction, appearing: true)), .scale(0.8)])
case .cover(let direction):
context.insertToViewFirst = false
context[toView]!.append(contentsOf: [.translate(shift(direction: direction, appearing: true)),
.shadowOpacity(0),
.beginWith(modifiers: shadowState),
.timingFunction(.deceleration)])
context[fromView]!.append(contentsOf: [.overlay(color: .black, opacity: 0.1),
.timingFunction(.deceleration)])
case .uncover(let direction):
context.insertToViewFirst = true
context[fromView]!.append(contentsOf: [.translate(shift(direction: direction, appearing: false)),
.shadowOpacity(0),
.beginWith(modifiers: shadowState)])
context[toView]!.append(contentsOf: [.overlay(color: .black, opacity: 0.1)])
case .pageIn(let direction):
context.insertToViewFirst = false
context[toView]!.append(contentsOf: [.translate(shift(direction: direction, appearing: true)),
.shadowOpacity(0),
.beginWith(modifiers: shadowState),
.timingFunction(.deceleration)])
context[fromView]!.append(contentsOf: [.scale(0.7),
.overlay(color: .black, opacity: 0.1),
.timingFunction(.deceleration)])
case .pageOut(let direction):
context.insertToViewFirst = true
context[fromView]!.append(contentsOf: [.translate(shift(direction: direction, appearing: false)),
.shadowOpacity(0),
.beginWith(modifiers: shadowState)])
context[toView]!.append(contentsOf: [.scale(0.7),
.overlay(color: .black, opacity: 0.1)])
case .fade:
// TODO: clean up this. overFullScreen logic shouldn't be here
if !(fromOverFullScreen && !presenting) {
context[toView] = [.fade]
}
#if os(tvOS)
context[fromView] = [.fade]
#else
if (!presenting && toOverFullScreen) || !fromView.isOpaque || (fromView.backgroundColor?.alphaComponent ?? 1) < 1 {
context[fromView] = [.fade]
}
#endif
context[toView]!.append(.durationMatchLongest)
context[fromView]!.append(.durationMatchLongest)
case .zoom:
context.insertToViewFirst = true
context[fromView]!.append(contentsOf: [.scale(1.3), .fade])
context[toView]!.append(contentsOf: [.scale(0.7)])
case .zoomOut:
context.insertToViewFirst = false
context[toView]!.append(contentsOf: [.scale(1.3), .fade])
context[fromView]!.append(contentsOf: [.scale(0.7)])
default:
fatalError("Not implemented")
}
}
}
#endif