forked from SwiftKickMobile/SwiftMessages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTopBottomAnimation.swift
250 lines (225 loc) · 9.36 KB
/
TopBottomAnimation.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
//
// TopBottomAnimation.swift
// SwiftMessages
//
// Created by Timothy Moose on 6/4/17.
// Copyright © 2017 SwiftKick Mobile. All rights reserved.
//
import UIKit
public class TopBottomAnimation: NSObject, Animator {
public enum Style {
case top
case bottom
}
public weak var delegate: AnimationDelegate?
public let style: Style
open var heightDimension: Dimension?
open var showDuration: TimeInterval = 0.4
open var hideDuration: TimeInterval = 0.2
open var springDamping: CGFloat = 0.8
open var closeSpeedThreshold: CGFloat = 750.0;
open var closePercentThreshold: CGFloat = 0.33;
open var closeAbsoluteThreshold: CGFloat = 75.0;
public private(set) lazy var panGestureRecognizer: UIPanGestureRecognizer = {
let pan = UIPanGestureRecognizer()
pan.addTarget(self, action: #selector(pan(_:)))
return pan
}()
weak var messageView: UIView?
weak var containerView: UIView?
var context: AnimationContext?
public init(style: Style) {
self.style = style
}
init(style: Style, delegate: AnimationDelegate) {
self.style = style
self.delegate = delegate
}
public func show(context: AnimationContext, completion: @escaping AnimationCompletion) {
NotificationCenter.default.addObserver(self, selector: #selector(adjustMargins), name: UIDevice.orientationDidChangeNotification, object: nil)
install(context: context)
showAnimation(completion: completion)
}
public func hide(context: AnimationContext, completion: @escaping AnimationCompletion) {
NotificationCenter.default.removeObserver(self)
let view = context.messageView
self.context = context
UIView.animate(withDuration: hideDuration, delay: 0, options: [.beginFromCurrentState, .curveEaseIn], animations: {
switch self.style {
case .top:
view.transform = CGAffineTransform(translationX: 0, y: -view.frame.height)
case .bottom:
view.transform = CGAffineTransform(translationX: 0, y: view.frame.maxY + view.frame.height)
}
}, completion: { completed in
#if SWIFTMESSAGES_APP_EXTENSIONS
completion(completed)
#else
// Fix #131 by always completing if application isn't active.
completion(completed || UIApplication.shared.applicationState != .active)
#endif
})
}
func install(context: AnimationContext) {
let view = context.messageView
let container = context.containerView
messageView = view
containerView = container
self.context = context
if let adjustable = context.messageView as? MarginAdjustable {
bounceOffset = adjustable.bounceAnimationOffset
}
if let sizeableView = view as? MessageSizeable & UIView {
container.install(sizeableView: sizeableView)
} else {
view.translatesAutoresizingMaskIntoConstraints = false
container.addSubview(view)
}
// Horizontal constraints
do {
view.leadingAnchor.constraint(equalTo: container.leadingAnchor)
.with(priority: .belowMessageSizeable - 1)
.isActive = true
view.centerXAnchor.constraint(equalTo: container.centerXAnchor)
.with(priority: .belowMessageSizeable)
.isActive = true
}
switch style {
case .top:
view.topAnchor.constraint(equalTo: container.topAnchor, constant: -bounceOffset)
.with(priority: .belowMessageSizeable)
.isActive = true
case .bottom:
view.bottomAnchor.constraint(equalTo: container.bottomAnchor, constant: bounceOffset)
.with(priority: .belowMessageSizeable)
.isActive = true
}
// Important to layout now in order to get the right safe area insets
container.layoutIfNeeded()
adjustMargins()
container.layoutIfNeeded()
let animationDistance = view.frame.height
switch style {
case .top:
view.transform = CGAffineTransform(translationX: 0, y: -animationDistance)
case .bottom:
view.transform = CGAffineTransform(translationX: 0, y: animationDistance)
}
if context.interactiveHide {
if let view = view as? BackgroundViewable {
view.backgroundView.addGestureRecognizer(panGestureRecognizer)
} else {
view.addGestureRecognizer(panGestureRecognizer)
}
}
if let view = view as? BackgroundViewable,
let cornerRoundingView = view.backgroundView as? CornerRoundingView,
cornerRoundingView.roundsLeadingCorners {
switch style {
case .top:
cornerRoundingView.roundedCorners = [.bottomLeft, .bottomRight]
case .bottom:
cornerRoundingView.roundedCorners = [.topLeft, .topRight]
}
}
}
@objc public func adjustMargins() {
guard let adjustable = messageView as? MarginAdjustable & UIView,
let context = context else { return }
adjustable.preservesSuperviewLayoutMargins = false
if #available(iOS 11, *) {
adjustable.insetsLayoutMarginsFromSafeArea = false
}
var layoutMargins = adjustable.defaultMarginAdjustment(context: context)
switch style {
case .top:
layoutMargins.top += bounceOffset
case .bottom:
layoutMargins.bottom += bounceOffset
}
adjustable.layoutMargins = layoutMargins
}
func showAnimation(completion: @escaping AnimationCompletion) {
guard let view = messageView else {
completion(false)
return
}
let animationDistance = abs(view.transform.ty)
// Cap the initial velocity at zero because the bounceOffset may not be great
// enough to allow for greater bounce induced by a quick panning motion.
let initialSpringVelocity = animationDistance == 0.0 ? 0.0 : min(0.0, closeSpeed / animationDistance)
UIView.animate(withDuration: showDuration, delay: 0.0, usingSpringWithDamping: springDamping, initialSpringVelocity: initialSpringVelocity, options: [.beginFromCurrentState, .curveLinear, .allowUserInteraction], animations: {
view.transform = .identity
}, completion: { completed in
// Fix #131 by always completing if application isn't active.
#if SWIFTMESSAGES_APP_EXTENSIONS
completion(completed)
#else
completion(completed || UIApplication.shared.applicationState != .active)
#endif
})
}
fileprivate var bounceOffset: CGFloat = 5
/*
MARK: - Pan to close
*/
fileprivate var closing = false
fileprivate var rubberBanding = false
fileprivate var closeSpeed: CGFloat = 0.0
fileprivate var closePercent: CGFloat = 0.0
fileprivate var panTranslationY: CGFloat = 0.0
@objc func pan(_ pan: UIPanGestureRecognizer) {
switch pan.state {
case .changed:
guard let view = messageView else { return }
let height = view.bounds.height - bounceOffset
if height <= 0 { return }
var velocity = pan.velocity(in: view)
var translation = pan.translation(in: view)
if case .top = style {
velocity.y *= -1.0
translation.y *= -1.0
}
var translationAmount = translation.y >= 0 ? translation.y : -pow(abs(translation.y), 0.7)
if !closing {
// Turn on rubber banding if background view is inset from message view.
if let background = (messageView as? BackgroundViewable)?.backgroundView, background != view {
switch style {
case .top:
rubberBanding = background.frame.minY > 0
case .bottom:
rubberBanding = background.frame.maxY < view.bounds.height
}
}
if !rubberBanding && translationAmount < 0 { return }
closing = true
delegate?.panStarted(animator: self)
}
if !rubberBanding && translationAmount < 0 { translationAmount = 0 }
switch style {
case .top:
view.transform = CGAffineTransform(translationX: 0, y: -translationAmount)
case .bottom:
view.transform = CGAffineTransform(translationX: 0, y: translationAmount)
}
closeSpeed = velocity.y
closePercent = translation.y / height
panTranslationY = translation.y
case .ended, .cancelled:
if closeSpeed > closeSpeedThreshold || closePercent > closePercentThreshold || panTranslationY > closeAbsoluteThreshold {
delegate?.hide(animator: self)
} else {
closing = false
rubberBanding = false
closeSpeed = 0.0
closePercent = 0.0
panTranslationY = 0.0
showAnimation(completion: { (completed) in
self.delegate?.panEnded(animator: self)
})
}
default:
break
}
}
}