-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
HoverView.swift
294 lines (246 loc) Β· 11 KB
/
HoverView.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
//
// HoverView.swift
// Hover
//
// Created by Pedro Carrasco on 11/07/2019.
// Copyright Β© 2019 Pedro Carrasco. All rights reserved.
//
import UIKit
// MARK: - HoverView
public class HoverView: UIView {
// MARK: Constant
private enum Constant {
static let animationDamping: CGFloat = 0.8
static let animationResponse: CGFloat = 0.5
static let animationDuration = 0.4
static let enableAnimationDuration = 0.2
static let anchorAnimationDuration = 0.15
static let interItemSpacing: CGFloat = 12.0
static let disabledAlpha: CGFloat = 0.75
static let disabledTransform = CGAffineTransform(scaleX: 0.9, y: 0.9)
}
// MARK: State
private enum State {
case none
case disappearing
case pending
}
// MARK: Outlets
private let button: HoverButton
private let itemViews: [HoverItemView]
private let itemsStackView: UIStackView = .create {
$0.spacing = Constant.interItemSpacing
$0.axis = .vertical
$0.isUserInteractionEnabled = false
}
private let dimView: DimView = .create {
$0.alpha = 0.0
}
// MARK: Constraints
private var stackViewXConstraint = NSLayoutConstraint()
private var stackViewYConstraint = NSLayoutConstraint()
// MARK: Properties
public var isEnabled = true {
didSet {
UIView.animate(withDuration: Constant.enableAnimationDuration) {
self.button.alpha = self.isEnabled ? 1.0 : Constant.disabledAlpha
self.button.transform = self.isEnabled ? .identity : Constant.disabledTransform
self.button.isEnabled = self.isEnabled
}
if !isEnabled {
animateState(to: false)
}
}
}
// MARK: Private Properties
private let anchors: [Anchor]
private let configuration: HoverConfiguration
private let items: [HoverItem]
private let panRecognizer = UIPanGestureRecognizer()
private var state: State = .none
private var isOpen = false
private var offset = CGPoint.zero
private var currentAnchor: Anchor {
didSet {
if state == .disappearing {
state = .pending
} else {
adapt(to: currentAnchor)
}
}
}
// MARK: Lifecycle
public init(with configuration: HoverConfiguration = .init(), items: [HoverItem] = .init()) {
guard !configuration.allowedPositions.isEmpty else {
fatalError("`allowedPositions` can't be empty")
}
self.anchors = configuration.allowedPositions.map(Anchor.init)
guard let currentAnchor = anchors.first(where: { $0.position == configuration.initialPosition }) else {
fatalError("`allowedPositions` must contain the `initalPosition`")
}
self.currentAnchor = currentAnchor
self.configuration = configuration
self.button = HoverButton(with: configuration.color, image: configuration.image, imageSizeRatio: configuration.imageSizeRatio)
self.itemViews = items.reversed().map { HoverItemView(with: $0, configuration: configuration.itemConfiguration) }
self.items = items
super.init(frame: .zero)
configure()
}
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func layoutSubviews() {
super.layoutSubviews()
button.center = currentAnchor.center
}
}
public extension HoverView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let hitView = super.hitTest(point, with: event)
if hitView == self {
onTouchInDim()
return nil
}
return hitView
}
}
// MARK: - Configuration
private extension HoverView {
func configure() {
addSubviews()
defineConstraints()
setupSubviews()
}
func addSubviews() {
add(layoutGuides: anchors.map { $0.guide })
add(views: dimView, itemsStackView, button)
itemsStackView.add(arrangedViews: itemViews, hidden: true)
}
func defineConstraints() {
anchors.forEach {
$0.position.configurePosition(of: $0.guide, inside: self, with: self.configuration.spacing)
NSLayoutConstraint.activate(
[
$0.guide.widthAnchor.constraint(equalToConstant: self.configuration.size),
$0.guide.heightAnchor.constraint(equalToConstant: self.configuration.size)
]
)
}
NSLayoutConstraint.activate(
[
button.widthAnchor.constraint(equalToConstant: configuration.size),
button.heightAnchor.constraint(equalToConstant: configuration.size),
dimView.topAnchor.constraint(equalTo: topAnchor),
dimView.bottomAnchor.constraint(equalTo: bottomAnchor),
dimView.leadingAnchor.constraint(equalTo: leadingAnchor),
dimView.trailingAnchor.constraint(equalTo: trailingAnchor)
]
)
adapt(to: currentAnchor)
}
func setupSubviews() {
dimView.backgroundColor = configuration.dimColor
button.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(onPan(from:))))
button.addTarget(self, action: #selector(onTapInButton), for: .touchUpInside)
itemViews.forEach { $0.onTap = onTapInButton }
}
}
// MARK: - Gestures
private extension HoverView {
func onTouchInDim() {
animateState(to: false)
}
@objc
func onTapInButton() {
animateState(to: !isOpen)
}
@objc
func onPan(from recognizer: UIPanGestureRecognizer) {
animateState(to: false)
let touchPoint = recognizer.location(in: self)
switch recognizer.state {
case .began:
offset = touchPoint - button.center
case .changed:
button.center = touchPoint - offset
case .ended, .cancelled:
let velocity = recognizer.velocity(in: self)
let projectedPosition = Calculator.project(velocity: velocity, decelerationRate: .normal, into: button.center)
currentAnchor = Calculator.nearestPoint(from: projectedPosition, to: anchors)
let relativeVelocity = Calculator.relativeVelocity(for: velocity, from: button.center, to: currentAnchor.center)
let timingParameters = UISpringTimingParameters(damping: Constant.animationDamping,
response: Constant.animationResponse,
initialVelocity: relativeVelocity)
let animator = UIViewPropertyAnimator(duration: Constant.anchorAnimationDuration, timingParameters: timingParameters)
animator.addAnimations { self.button.center = self.currentAnchor.center }
animator.startAnimation()
default:
break
}
}
}
// MARK: - Animations
private extension HoverView {
func animateState(to isOpen: Bool) {
guard self.isOpen != isOpen else { return }
self.isOpen = isOpen
UIImpactFeedbackGenerator(style: .light).impactOccurred()
if isOpen {
animate(isOpening: true, anchor: currentAnchor)
} else {
state = .disappearing
animate(isOpening: false, anchor: currentAnchor) {
if self.state == .pending {
self.adapt(to: self.currentAnchor)
}
self.state = .none
}
}
}
private func animate(isOpening: Bool, anchor: Anchor, completion: (() -> Void)? = nil) {
itemsStackView.isUserInteractionEnabled = isOpening
UIViewPropertyAnimator(duration: Constant.animationDuration, curve: .easeInOut) {
self.dimView.alpha = isOpening ? 1.0 : 0.0
}.startAnimation()
anchor.position.yOrientation.reverseArrayIfNeeded(itemsStackView.arrangedSubviews).enumerated().forEach { (index, view) in
let translationTransform = Calculator.translation(for: index, anchor: anchor)
view.transform = isOpening ? translationTransform : .identity
let animator = UIViewPropertyAnimator(duration: Constant.animationDuration, dampingRatio: Constant.animationDamping) {
view.transform = isOpening ? .identity : translationTransform
view.alpha = isOpening ? 1.0 : 0.0
}
animator.addCompletion { _ in
if index == self.itemsStackView.arrangedSubviews.count - 1 {
completion?()
}
}
animator.startAnimation(afterDelay: Calculator.delay(for: index))
}
}
}
// MARK: - Conditional Constraints
private extension HoverView {
func adapt(to anchor: Anchor) {
NSLayoutConstraint.deactivate([stackViewXConstraint, stackViewYConstraint])
switch anchor.position {
case .topLeft:
itemsStackView.add(arrangedViews: itemViews.reversed(), hidden: true)
stackViewXConstraint = itemsStackView.leadingAnchor.constraint(equalTo: anchor.guide.leadingAnchor, constant: self.configuration.itemConfiguration.margin)
stackViewYConstraint = itemsStackView.topAnchor.constraint(equalTo: currentAnchor.guide.bottomAnchor, constant: self.configuration.spacing)
case .topRight:
itemsStackView.add(arrangedViews: itemViews.reversed(), hidden: true)
stackViewXConstraint = itemsStackView.trailingAnchor.constraint(equalTo: anchor.guide.trailingAnchor, constant: -self.configuration.itemConfiguration.margin)
stackViewYConstraint = itemsStackView.topAnchor.constraint(equalTo: currentAnchor.guide.bottomAnchor, constant: self.configuration.spacing)
case .bottomLeft:
itemsStackView.add(arrangedViews: itemViews, hidden: true)
stackViewXConstraint = itemsStackView.leadingAnchor.constraint(equalTo: anchor.guide.leadingAnchor, constant: self.configuration.itemConfiguration.margin)
stackViewYConstraint = itemsStackView.bottomAnchor.constraint(equalTo: currentAnchor.guide.topAnchor, constant: -self.configuration.spacing)
case .bottomRight:
itemsStackView.add(arrangedViews: itemViews, hidden: true)
stackViewXConstraint = itemsStackView.trailingAnchor.constraint(equalTo: anchor.guide.trailingAnchor, constant: -self.configuration.itemConfiguration.margin)
stackViewYConstraint = itemsStackView.bottomAnchor.constraint(equalTo: currentAnchor.guide.topAnchor, constant: -self.configuration.spacing)
}
NSLayoutConstraint.activate([stackViewXConstraint, stackViewYConstraint])
itemViews.forEach { $0.orientation = currentAnchor.position.xOrientation }
}
}