-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathNavigationTransitionDelegate.swift
162 lines (136 loc) · 5.37 KB
/
NavigationTransitionDelegate.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
import Animation
import Animator
import NavigationTransition
import UIKit
final class NavigationTransitionDelegate: NSObject, UINavigationControllerDelegate {
var transition: AnyNavigationTransition
private weak var baseDelegate: UINavigationControllerDelegate?
var interactionController: UIPercentDrivenInteractiveTransition?
init(transition: AnyNavigationTransition, baseDelegate: UINavigationControllerDelegate?) {
self.transition = transition
self.baseDelegate = baseDelegate
}
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
baseDelegate?.navigationController?(navigationController, willShow: viewController, animated: animated)
}
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
baseDelegate?.navigationController?(navigationController, didShow: viewController, animated: animated)
}
func navigationController(_ navigationController: UINavigationController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
if !transition.isDefault {
return interactionController
} else {
return nil
}
}
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
if
!transition.isDefault,
let animation = transition.animation,
let operation = NavigationTransitionOperation(operation)
{
return NavigationTransitionAnimatorProvider(
transition: transition,
animation: animation,
operation: operation
)
} else {
return nil
}
}
}
final class NavigationTransitionAnimatorProvider: NSObject, UIViewControllerAnimatedTransitioning {
let transition: AnyNavigationTransition
let animation: Animation
let operation: NavigationTransitionOperation
init(transition: AnyNavigationTransition, animation: Animation, operation: NavigationTransitionOperation) {
self.transition = transition
self.animation = animation
self.operation = operation
}
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
animation.duration
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
transitionAnimator(for: transitionContext).startAnimation()
}
func interruptibleAnimator(using transitionContext: UIViewControllerContextTransitioning) -> UIViewImplicitlyAnimating {
transitionAnimator(for: transitionContext)
}
func animationEnded(_ transitionCompleted: Bool) {
cachedAnimators.removeAll(keepingCapacity: true)
}
private var cachedAnimators: [ObjectIdentifier: UIViewPropertyAnimator] = .init(minimumCapacity: 1)
private func transitionAnimator(for transitionContext: UIViewControllerContextTransitioning) -> UIViewPropertyAnimator {
if let cached = cachedAnimators[ObjectIdentifier(transitionContext)] {
return cached
}
let animator = UIViewPropertyAnimator(
duration: transitionDuration(using: transitionContext),
timingParameters: animation.timingParameters
)
cachedAnimators[ObjectIdentifier(transitionContext)] = animator
let container = transitionContext.containerView
guard
let fromUIView = transitionContext.view(forKey: .from),
let toUIView = transitionContext.view(forKey: .to)
else {
return animator
}
fromUIView.isUserInteractionEnabled = false
toUIView.isUserInteractionEnabled = false
switch transition.handler {
case .transient(let handler):
if let (fromView, toView) = transientViews(
for: handler,
animator: animator,
context: (container, fromUIView, toUIView)
) {
for view in [fromView, toView] {
view.setUIViewProperties(to: \.initial)
animator.addAnimations { view.setUIViewProperties(to: \.animation) }
animator.addCompletion { _ in
if transitionContext.transitionWasCancelled {
view.resetUIViewProperties()
} else {
view.setUIViewProperties(to: \.completion)
}
}
}
}
case .primitive(let handler):
handler(animator, operation, transitionContext)
}
animator.addCompletion { _ in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
fromUIView.isUserInteractionEnabled = true
toUIView.isUserInteractionEnabled = true
// iOS 16 workaround to nudge views into becoming responsive after transition
if transitionContext.transitionWasCancelled {
fromUIView.removeFromSuperview()
container.addSubview(fromUIView)
} else {
toUIView.removeFromSuperview()
container.addSubview(toUIView)
}
}
return animator
}
private func transientViews(
for handler: AnyNavigationTransition.TransientHandler,
animator: Animator,
context: (container: UIView, fromUIView: UIView, toUIView: UIView)
) -> (fromView: AnimatorTransientView, toView: AnimatorTransientView)? {
let (container, fromUIView, toUIView) = context
switch operation {
case .push:
container.insertSubview(toUIView, aboveSubview: fromUIView)
case .pop:
container.insertSubview(toUIView, belowSubview: fromUIView)
}
let fromView = AnimatorTransientView(fromUIView)
let toView = AnimatorTransientView(toUIView)
handler(fromView, toView, operation, container)
return (fromView, toView)
}
}