-
Notifications
You must be signed in to change notification settings - Fork 41
/
SheetTransition.swift
183 lines (159 loc) · 6.73 KB
/
SheetTransition.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
import UIKit
final class SheetTransition: NSObject {
// MARK: - Internal properties
weak var presenter: UIViewController?
var presenting = true
private var extendedLayoutIncludesOpaqueBars: Bool?
// MARK: - Initialization
private let contentViewController: SheetContentViewController
private let options: SheetOptions
init(
contentViewController: SheetContentViewController,
options: SheetOptions
) {
self.contentViewController = contentViewController
self.options = options
super.init()
}
}
// MARK: - Internal helpers
extension SheetTransition {
func restorePresenter(
_ presenter: UIViewController,
animated: Bool = true,
animations: (() -> Void)? = nil,
completion: ((Bool) -> Void)? = nil
) {
extendedLayoutIncludesOpaqueBars.map {
presenter.extendedLayoutIncludesOpaqueBars = $0
}
UIView.animate(
withDuration: options.transitionDuration,
animations: {
presenter.view.layer.transform = CATransform3DIdentity
presenter.view.layer.cornerRadius = 0
animations?()
},
completion: {
completion?($0)
}
)
}
func setPresenter(
percentComplete: CGFloat
) {
guard let presenter = presenter else { return }
let scale: CGFloat = min(1, 0.92 + (0.08 * percentComplete))
let topSafeArea: CGFloat
if #available(iOS 11.0, *) {
topSafeArea = UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.safeAreaInsets.top ?? 0
} else {
topSafeArea = UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.layoutMargins.top ?? 0
}
presenter.view.layer.transform = CATransform3DConcat(
CATransform3DMakeTranslation(0, (1 - percentComplete) * topSafeArea / 2, 0),
CATransform3DMakeScale(scale, scale, 1)
)
presenter.view.layer.cornerRadius = options.presentingViewCornerRadius * (1 - percentComplete)
}
}
// MARK: - UIViewControllerAnimatedTransitioning
extension SheetTransition: UIViewControllerAnimatedTransitioning {
func animateTransition(
using transitionContext: UIViewControllerContextTransitioning
) {
let containerView = transitionContext.containerView
if presenting {
guard let presenter = transitionContext.viewController(forKey: .from),
let sheet = transitionContext.viewController(forKey: .to) as? SheetViewController else {
transitionContext.completeTransition(true)
return
}
self.presenter = presenter
if
!UINavigationBar.appearance().isTranslucent,
presenter.extendedLayoutIncludesOpaqueBars == false,
let container = presenter as? UINavigationController,
let top = container.topViewController,
container.navigationBar.backgroundImage(for: .default) == nil
{
top.extendedLayoutIncludesOpaqueBars = true
extendedLayoutIncludesOpaqueBars = false
}
contentViewController.view.transform = .identity
containerView.addSubview(sheet.view)
sheet.view.setNeedsLayout()
sheet.view.layoutIfNeeded()
contentViewController.updatePreferredHeight()
sheet.resize(to: .intrinsic, duration: options.animationDuration, options: options.animationOptions)
let contentView = contentViewController.contentView
contentView.transform = CGAffineTransform(translationX: 0, y: contentView.bounds.height)
sheet.overlayView.alpha = 0
let heightPercent = contentView.bounds.height / UIScreen.main.bounds.height
// Animate shadow and background view
UIView.animate(
withDuration: options.transitionDuration * 0.6,
delay: 0,
options: [.curveEaseOut],
animations: {
let topSafeArea: CGFloat
if #available(iOS 11.0, *) {
topSafeArea = UIApplication.shared.windows
.first(where: { $0.isKeyWindow })?.safeAreaInsets.top ?? 0
} else {
topSafeArea = UIApplication.shared.windows
.first(where: { $0.isKeyWindow })?.layoutMargins.top ?? 0
}
presenter.view.layer.transform = CATransform3DConcat(
CATransform3DMakeTranslation(0, topSafeArea / 2, 0),
CATransform3DMakeScale(0.92, 0.92, 1)
)
presenter.view.layer.cornerRadius = self.options.presentingViewCornerRadius
presenter.view.layer.masksToBounds = true
sheet.overlayView.alpha = 1
},
completion: nil
)
// Animate the view in
UIView.animate(
withDuration: options.transitionDuration,
delay: 0,
usingSpringWithDamping: options.transitionDampening + ((heightPercent - 0.2) * 1.25 * 0.17),
initialSpringVelocity: options.transitionVelocity * heightPercent,
options: options.animationOptions,
animations: {
contentView.transform = .identity
},
completion: { _ in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
)
} else {
guard let presenter = transitionContext.viewController(forKey: .to),
let sheet = transitionContext.viewController(forKey: .from) as? SheetViewController else {
transitionContext.completeTransition(true)
return
}
containerView.addSubview(sheet.view)
let contentView = contentViewController.contentView
restorePresenter(
presenter,
animations: {
contentView.transform = CGAffineTransform(
translationX: 0,
y: contentView.bounds.height
)
sheet.overlayView.alpha = 0
},
completion: { _ in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
)
}
}
func transitionDuration(
using transitionContext: UIViewControllerContextTransitioning?
) -> TimeInterval {
options.transitionDuration
}
}