-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAPRedorderableStackView.swift
More file actions
353 lines (282 loc) · 14.6 KB
/
APRedorderableStackView.swift
File metadata and controls
353 lines (282 loc) · 14.6 KB
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
//
// APRedorderableStackView.swift
// ReorderStackView
//
// Created by Clay Ellis on 10/16/15.
// Copyright © 2015 Appsidian. All rights reserved.
//
import UIKit
@objc
public protocol APStackViewReorderDelegate {
/// didBeginReordering - called when reordering begins
@objc optional func didBeginReordering()
/// Whenever a user drags a subview for a reordering, the delegate is told whether the direction
/// was up or down, as well as what the max and min Y values are of the subview
@objc optional func didDragToReorder(inUpDirection up: Bool, maxY: CGFloat, minY: CGFloat)
/// didReorder - called whenever a subview was reordered (returns the new index)
/// didEndReordering - called when reordering ends
@objc optional func didEndReordering()
}
public class APRedorderableStackView: UIStackView, UIGestureRecognizerDelegate {
/// Setting `reorderdingEnabled` to `true` enables a drag to reorder behavior like `UITableView`
public var reorderingEnabled = false {
didSet {
self.setReorderingEnabled(self.reorderingEnabled)
}
}
public var reorderDelegate: APStackViewReorderDelegate?
// Gesture recognizers
fileprivate var longPressGRS = [UILongPressGestureRecognizer]()
// Views for reordering
fileprivate var temporaryView: UIView!
fileprivate var temporaryViewForShadow: UIView!
fileprivate var actualView: UIView!
// Values for reordering
fileprivate var reordering = false
fileprivate var finalReorderFrame: CGRect!
fileprivate var originalPosition: CGPoint!
fileprivate var pointForReordering: CGPoint!
// Appearance Constants
public var clipsToBoundsWhileReordering = false
public var cornerRadii: CGFloat = 5
public var temporaryViewScale: CGFloat = 1.05
public var otherViewsScale: CGFloat = 0.97
public var temporaryViewAlpha: CGFloat = 0.9
/// The gap created once the long press drag is triggered
public var dragHintSpacing: CGFloat = 5
public var longPressMinimumPressDuration = 0.2 {
didSet {
self.updateMinimumPressDuration()
}
}
// MARK:- Reordering Methods
// ---------------------------------------------------------------------------------------------
override public func addArrangedSubview(_ view: UIView) {
super.addArrangedSubview(view)
self.addLongPressGestureRecognizerForReorderingToView(view)
}
fileprivate func addLongPressGestureRecognizerForReorderingToView(_ view: UIView) {
let longPressGR = UILongPressGestureRecognizer(target: self, action: #selector(APRedorderableStackView.handleLongPress(_:)))
longPressGR.delegate = self
longPressGR.minimumPressDuration = self.longPressMinimumPressDuration
longPressGR.isEnabled = self.reorderingEnabled
view.addGestureRecognizer(longPressGR)
self.longPressGRS.append(longPressGR)
}
fileprivate func setReorderingEnabled(_ enabled: Bool) {
for longPressGR in self.longPressGRS {
longPressGR.isEnabled = enabled
}
}
fileprivate func updateMinimumPressDuration() {
for longPressGR in self.longPressGRS {
longPressGR.minimumPressDuration = self.longPressMinimumPressDuration
}
}
@objc internal func handleLongPress(_ gr: UILongPressGestureRecognizer) {
if gr.state == .began {
self.reordering = true
self.reorderDelegate?.didBeginReordering?()
self.actualView = gr.view!
self.originalPosition = gr.location(in: self)
self.originalPosition.y -= self.dragHintSpacing
self.pointForReordering = self.originalPosition
self.prepareForReordering()
} else if gr.state == .changed {
// Drag the temporaryView
let newLocation = gr.location(in: self)
let xOffset = newLocation.x - originalPosition.x
let yOffset = newLocation.y - originalPosition.y
let translation = CGAffineTransform(translationX: xOffset, y: yOffset)
// Replicate the scale that was initially applied in perpareForReordering:
let scale = CGAffineTransform(scaleX: self.temporaryViewScale, y: self.temporaryViewScale)
self.temporaryView.transform = scale.concatenating(translation)
self.temporaryViewForShadow.transform = translation
// Use the midY of the temporaryView to determine the dragging direction, location
// maxY and minY are used in the delegate call didDragToReorder
let maxY = self.temporaryView.frame.maxY
let midY = self.temporaryView.frame.midY
let minY = self.temporaryView.frame.minY
let index = self.indexOfArrangedSubview(self.actualView)
if midY > self.pointForReordering.y {
// Dragging the view down
self.reorderDelegate?.didDragToReorder?(inUpDirection: false, maxY: maxY, minY: minY)
if let nextView = self.getNextViewInStack(usingIndex: index) {
if midY > nextView.frame.midY {
// Swap the two arranged subviews
UIView.animate(withDuration: 0.2, animations: {
self.insertArrangedSubview(nextView, at: index)
self.insertArrangedSubview(self.actualView, at: index + 1)
})
self.finalReorderFrame = self.actualView.frame
self.pointForReordering.y = self.actualView.frame.midY
}
}
} else {
// Dragging the view up
self.reorderDelegate?.didDragToReorder?(inUpDirection: true, maxY: maxY, minY: minY)
if let previousView = self.getPreviousViewInStack(usingIndex: index) {
if midY < previousView.frame.midY {
// Swap the two arranged subviews
UIView.animate(withDuration: 0.2, animations: {
self.insertArrangedSubview(previousView, at: index)
self.insertArrangedSubview(self.actualView, at: index - 1)
})
self.finalReorderFrame = self.actualView.frame
self.pointForReordering.y = self.actualView.frame.midY
}
}
}
} else if gr.state == .ended || gr.state == .cancelled || gr.state == .failed {
self.cleanupUpAfterReordering()
self.reordering = false
self.reorderDelegate?.didEndReordering?()
}
}
fileprivate func prepareForReordering() {
self.clipsToBounds = self.clipsToBoundsWhileReordering
// Configure the temporary view
self.temporaryView = self.actualView.snapshotView(afterScreenUpdates: true)
self.temporaryView.frame = self.actualView.frame
self.finalReorderFrame = self.actualView.frame
self.addSubview(self.temporaryView)
// Hide the actual view and grow the temporaryView
self.actualView.alpha = 0
UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [.allowUserInteraction, .beginFromCurrentState], animations: {
self.styleViewsForReordering()
}, completion: nil)
}
fileprivate func cleanupUpAfterReordering() {
UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [.allowUserInteraction, .beginFromCurrentState], animations: {
self.styleViewsForEndReordering()
}, completion: { (Bool) -> Void in
// Hide the temporaryView, show the actualView
self.temporaryViewForShadow.removeFromSuperview()
self.temporaryView.removeFromSuperview()
self.actualView.alpha = 1
self.clipsToBounds = !self.clipsToBoundsWhileReordering
})
}
// MARK:- View Styling Methods
// ---------------------------------------------------------------------------------------------
fileprivate func styleViewsForReordering() {
let roundKey = "Round"
let round = CABasicAnimation(keyPath: "cornerRadius")
round.fromValue = 0
round.toValue = self.cornerRadii
round.duration = 0.1
round.isRemovedOnCompletion = false
round.fillMode = CAMediaTimingFillMode.forwards
// Grow, hint with offset, fade, round the temporaryView
let scale = CGAffineTransform(scaleX: self.temporaryViewScale, y: self.temporaryViewScale)
let translation = CGAffineTransform(translationX: 0, y: self.dragHintSpacing)
self.temporaryView.transform = scale.concatenating(translation)
self.temporaryView.alpha = self.temporaryViewAlpha
self.temporaryView.layer.add(round, forKey: roundKey)
self.temporaryView.clipsToBounds = true // Clips to bounds to apply corner radius
// Shadow
self.temporaryViewForShadow = UIView(frame: self.temporaryView.frame)
self.insertSubview(self.temporaryViewForShadow, belowSubview: self.temporaryView)
self.temporaryViewForShadow.layer.shadowColor = UIColor.black.cgColor
self.temporaryViewForShadow.layer.shadowPath = UIBezierPath(roundedRect: self.temporaryView.bounds, cornerRadius: self.cornerRadii).cgPath
// Shadow animations
let shadowOpacityKey = "ShadowOpacity"
let shadowOpacity = CABasicAnimation(keyPath: "shadowOpacity")
shadowOpacity.fromValue = 0
shadowOpacity.toValue = 0.2
shadowOpacity.duration = 0.2
shadowOpacity.isRemovedOnCompletion = false
shadowOpacity.fillMode = CAMediaTimingFillMode.forwards
let shadowOffsetKey = "ShadowOffset"
let shadowOffset = CABasicAnimation(keyPath: "shadowOffset.height")
shadowOffset.fromValue = 0
shadowOffset.toValue = 50
shadowOffset.duration = 0.2
shadowOffset.isRemovedOnCompletion = false
shadowOffset.fillMode = CAMediaTimingFillMode.forwards
let shadowRadiusKey = "ShadowRadius"
let shadowRadius = CABasicAnimation(keyPath: "shadowRadius")
shadowRadius.fromValue = 0
shadowRadius.toValue = 20
shadowRadius.duration = 0.2
shadowRadius.isRemovedOnCompletion = false
shadowRadius.fillMode = CAMediaTimingFillMode.forwards
self.temporaryViewForShadow.layer.add(shadowOpacity, forKey: shadowOpacityKey)
self.temporaryViewForShadow.layer.add(shadowOffset, forKey: shadowOffsetKey)
self.temporaryViewForShadow.layer.add(shadowRadius, forKey: shadowRadiusKey)
// Scale down and round other arranged subviews
for subview in self.arrangedSubviews {
if subview != self.actualView {
subview.layer.add(round, forKey: roundKey)
subview.transform = CGAffineTransform(scaleX: self.otherViewsScale, y: self.otherViewsScale)
}
}
}
fileprivate func styleViewsForEndReordering() {
let squareKey = "Square"
let square = CABasicAnimation(keyPath: "cornerRadius")
square.fromValue = self.cornerRadii
square.toValue = 0
square.duration = 0.1
square.isRemovedOnCompletion = false
square.fillMode = CAMediaTimingFillMode.forwards
// Return drag view to original appearance
self.temporaryView.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
self.temporaryView.frame = self.finalReorderFrame
self.temporaryView.alpha = 1.0
self.temporaryView.layer.add(square, forKey: squareKey)
// Shadow animations
let shadowOpacityKey = "ShadowOpacity"
let shadowOpacity = CABasicAnimation(keyPath: "shadowOpacity")
shadowOpacity.fromValue = 0.2
shadowOpacity.toValue = 0
shadowOpacity.duration = 0.2
shadowOpacity.isRemovedOnCompletion = false
shadowOpacity.fillMode = CAMediaTimingFillMode.forwards
let shadowOffsetKey = "ShadowOffset"
let shadowOffset = CABasicAnimation(keyPath: "shadowOffset.height")
shadowOffset.fromValue = 50
shadowOffset.toValue = 0
shadowOffset.duration = 0.2
shadowOffset.isRemovedOnCompletion = false
shadowOffset.fillMode = CAMediaTimingFillMode.forwards
let shadowRadiusKey = "ShadowRadius"
let shadowRadius = CABasicAnimation(keyPath: "shadowRadius")
shadowRadius.fromValue = 20
shadowRadius.toValue = 0
shadowRadius.duration = 0.4
shadowRadius.isRemovedOnCompletion = false
shadowRadius.fillMode = CAMediaTimingFillMode.forwards
self.temporaryViewForShadow.layer.add(shadowOpacity, forKey: shadowOpacityKey)
self.temporaryViewForShadow.layer.add(shadowOffset, forKey: shadowOffsetKey)
self.temporaryViewForShadow.layer.add(shadowRadius, forKey: shadowRadiusKey)
// Return other arranged subviews to original appearances
for subview in self.arrangedSubviews {
UIView.animate(withDuration: 0.3, animations: {
subview.layer.add(square, forKey: squareKey)
subview.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
})
}
}
// MARK:- Stack View Helper Methods
// ---------------------------------------------------------------------------------------------
fileprivate func indexOfArrangedSubview(_ view: UIView) -> Int {
for (index, subview) in self.arrangedSubviews.enumerated() {
if view == subview {
return index
}
}
return 0
}
fileprivate func getPreviousViewInStack(usingIndex index: Int) -> UIView? {
if index == 0 { return nil }
return self.arrangedSubviews[index - 1]
}
fileprivate func getNextViewInStack(usingIndex index: Int) -> UIView? {
if index == self.arrangedSubviews.count - 1 { return nil }
return self.arrangedSubviews[index + 1]
}
override public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return !self.reordering
}
}