forked from nicolasgomollon/LPRTableView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLPRTableView.swift
More file actions
350 lines (278 loc) · 12.9 KB
/
LPRTableView.swift
File metadata and controls
350 lines (278 loc) · 12.9 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
//
// LPRTableView.swift
// LPRTableView
//
// Objective-C code Copyright (c) 2013 Ben Vogelzang. All rights reserved.
// Swift adaptation Copyright (c) 2014 Nicolas Gomollon. All rights reserved.
//
import Foundation
import QuartzCore
import UIKit
/** The delegate of a LPRTableView object can adopt the LPRTableViewDelegate protocol. Optional methods of the protocol allow the delegate to modify a cell visually before dragging occurs, or to be notified when a cell is about to be dragged or about to be dropped. */
@objc
public protocol LPRTableViewDelegate: NSObjectProtocol {
/** Provides the delegate a chance to modify the cell visually before dragging occurs. Defaults to using the cell as-is if not implemented. */
optional func tableView(tableView: UITableView!, draggingCell cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) -> UITableViewCell
/** Called within an animation block when the dragging view is about to show. */
optional func tableView(tableView: UITableView!, showDraggingView view: UIView, atIndexPath indexPath: NSIndexPath)
/** Called within an animation block when the dragging view is about to hide. */
optional func tableView(tableView: UITableView!, hideDraggingView view: UIView, atIndexPath indexPath: NSIndexPath)
}
public class LPRTableView: UITableView {
/** The object that acts as the delegate of the receiving table view. */
public var longPressReorderDelegate: LPRTableViewDelegate!
private var longPressGestureRecognizer: UILongPressGestureRecognizer!
private var initialIndexPath: NSIndexPath?
private var currentLocationIndexPath: NSIndexPath?
private var draggingView: UIView?
private var scrollRate = 0.0
private var scrollDisplayLink: CADisplayLink?
/** A Bool property that indicates whether long press to reorder is enabled. */
public var longPressReorderEnabled: Bool {
get {
return longPressGestureRecognizer.enabled
}
set {
longPressGestureRecognizer.enabled = newValue
}
}
public convenience init() {
self.init(frame: CGRectZero)
}
public convenience override init(frame: CGRect) {
self.init(frame: frame, style: .Plain)
}
public override init(frame: CGRect, style: UITableViewStyle) {
super.init(frame: frame, style: style)
initialize()
}
public required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initialize()
}
private func initialize() {
longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "_longPress:")
addGestureRecognizer(longPressGestureRecognizer)
}
}
extension LPRTableView {
private func canMoveRowAt(#indexPath: NSIndexPath) -> Bool {
return (dataSource?.respondsToSelector("tableView:canMoveRowAtIndexPath:") == false) || (dataSource?.tableView?(self, canMoveRowAtIndexPath: indexPath) == true)
}
private func cancelGesture() {
longPressGestureRecognizer.enabled = false
longPressGestureRecognizer.enabled = true
}
internal func _longPress(gesture: UILongPressGestureRecognizer) {
let location = gesture.locationInView(self)
let indexPath = indexPathForRowAtPoint(location)
let sections = numberOfSections()
var rows = 0
for i in 0..<sections {
rows += numberOfRowsInSection(i)
}
// Get out of here if the long press was not on a valid row or our table is empty
// or the dataSource tableView:canMoveRowAtIndexPath: doesn't allow moving the row.
if (rows == 0) ||
((gesture.state == UIGestureRecognizerState.Began) && (indexPath == nil)) ||
((gesture.state == UIGestureRecognizerState.Ended) && (currentLocationIndexPath == nil)) ||
((gesture.state == UIGestureRecognizerState.Began) && !canMoveRowAt(indexPath: indexPath!)) {
cancelGesture()
return
}
// Started.
if gesture.state == .Began {
if let indexPath = indexPath {
if var cell = cellForRowAtIndexPath(indexPath) {
cell.setSelected(false, animated: false)
cell.setHighlighted(false, animated: false)
// Create the view that will be dragged around the screen.
if (draggingView == nil) {
if let draggingCell = longPressReorderDelegate?.tableView?(self, draggingCell: cell, atIndexPath: indexPath) {
cell = draggingCell
}
// Make an image from the pressed table view cell.
UIGraphicsBeginImageContextWithOptions(cell.bounds.size, false, 0.0)
cell.layer.renderInContext(UIGraphicsGetCurrentContext())
var cellImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
draggingView = UIImageView(image: cellImage)
if var draggingView = draggingView {
addSubview(draggingView)
let rect = rectForRowAtIndexPath(indexPath)
draggingView.frame = CGRectOffset(draggingView.bounds, rect.origin.x, rect.origin.y)
UIView.beginAnimations("LongPressReorder-ShowDraggingView", context: nil)
longPressReorderDelegate?.tableView?(self, showDraggingView: draggingView, atIndexPath: indexPath)
UIView.commitAnimations()
// Add drop shadow to image and lower opacity.
draggingView.layer.masksToBounds = false
draggingView.layer.shadowColor = UIColor.blackColor().CGColor
draggingView.layer.shadowOffset = CGSizeZero
draggingView.layer.shadowRadius = 4.0
draggingView.layer.shadowOpacity = 0.7
draggingView.layer.opacity = 0.85
// Zoom image towards user.
UIView.beginAnimations("LongPressReorder-Zoom", context: nil)
draggingView.transform = CGAffineTransformMakeScale(1.1, 1.1)
draggingView.center = CGPointMake(center.x, location.y)
UIView.commitAnimations()
}
}
cell.hidden = true
currentLocationIndexPath = indexPath
initialIndexPath = indexPath
// Enable scrolling for cell.
scrollDisplayLink = CADisplayLink(target: self, selector: "_scrollTableWithCell:")
scrollDisplayLink?.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSDefaultRunLoopMode)
}
}
}
// Dragging.
else if gesture.state == .Changed {
if var draggingView = draggingView {
// Update position of the drag view,
// but don't let it go past the top or the bottom too far.
if (location.y >= 0.0) && (location.y <= contentSize.height + 50.0) {
draggingView.center = CGPointMake(center.x, location.y)
}
}
var rect = bounds
// Adjust rect for content inset, as we will use it below for calculating scroll zones.
rect.size.height -= contentInset.top
updateCurrentLocation(gesture)
// Tell us if we should scroll, and in which direction.
let scrollZoneHeight = rect.size.height / 6.0
let bottomScrollBeginning = contentOffset.y + contentInset.top + rect.size.height - scrollZoneHeight
let topScrollBeginning = contentOffset.y + contentInset.top + scrollZoneHeight
// We're in the bottom zone.
if location.y >= bottomScrollBeginning {
scrollRate = Double(location.y - bottomScrollBeginning) / Double(scrollZoneHeight)
}
// We're in the top zone.
else if location.y <= topScrollBeginning {
scrollRate = Double(location.y - topScrollBeginning) / Double(scrollZoneHeight)
}
else {
scrollRate = 0.0
}
}
// Dropped.
else if gesture.state == .Ended {
// Remove scrolling CADisplayLink.
scrollDisplayLink?.invalidate()
scrollDisplayLink = nil
scrollRate = 0.0
// Animate the drag view to the newly hovered cell.
UIView.animateWithDuration(0.3,
animations: { [unowned self] in
if var draggingView = self.draggingView {
if let currentLocationIndexPath = self.currentLocationIndexPath {
UIView.beginAnimations("LongPressReorder-HideDraggingView", context: nil)
self.longPressReorderDelegate?.tableView?(self, hideDraggingView: draggingView, atIndexPath: currentLocationIndexPath)
UIView.commitAnimations()
let rect = self.rectForRowAtIndexPath(currentLocationIndexPath)
draggingView.transform = CGAffineTransformIdentity
draggingView.frame = CGRectOffset(draggingView.bounds, rect.origin.x, rect.origin.y)
}
}
},
completion: { [unowned self] (finished: Bool) in
if var draggingView = self.draggingView {
draggingView.removeFromSuperview()
}
// Reload the rows that were affected just to be safe.
if let visibleRows = self.indexPathsForVisibleRows() {
self.reloadRowsAtIndexPaths(visibleRows, withRowAnimation: .None)
}
self.currentLocationIndexPath = nil
self.draggingView = nil
})
}
}
private func updateCurrentLocation(gesture: UILongPressGestureRecognizer) {
let location = gesture.locationInView(self)
if var indexPath = indexPathForRowAtPoint(location) {
if let iIndexPath = initialIndexPath {
if let ip = delegate?.tableView?(self, targetIndexPathForMoveFromRowAtIndexPath: iIndexPath, toProposedIndexPath: indexPath) {
indexPath = ip
}
}
if let clIndexPath = currentLocationIndexPath {
let oldHeight = rectForRowAtIndexPath(clIndexPath).size.height
let newHeight = rectForRowAtIndexPath(indexPath).size.height
if ((indexPath != clIndexPath) &&
(gesture.locationInView(cellForRowAtIndexPath(indexPath)).y > (newHeight - oldHeight))) &&
canMoveRowAt(indexPath: indexPath) {
beginUpdates()
moveRowAtIndexPath(clIndexPath, toIndexPath: indexPath)
dataSource?.tableView?(self, moveRowAtIndexPath: clIndexPath, toIndexPath: indexPath)
currentLocationIndexPath = indexPath
endUpdates()
}
}
}
}
internal func _scrollTableWithCell(sender: CADisplayLink) {
if let gesture = longPressGestureRecognizer {
let location = gesture.locationInView(self)
if !(location.y.isNaN || location.x.isNaN) { //explicitly check for out-of-bound touch
let yOffset = Double(contentOffset.y) + scrollRate * 10.0
var newOffset = CGPointMake(contentOffset.x, CGFloat(yOffset))
if newOffset.y < -contentInset.top {
newOffset.y = -contentInset.top
} else if (contentSize.height + contentInset.bottom) < frame.size.height {
newOffset = contentOffset
} else if newOffset.y > ((contentSize.height + contentInset.bottom) - frame.size.height) {
newOffset.y = (contentSize.height + contentInset.bottom) - frame.size.height
}
contentOffset = newOffset
if let draggingView = draggingView {
if (location.y >= 0) && (location.y <= (contentSize.height + 50.0)) {
draggingView.center = CGPointMake(center.x, location.y)
}
}
updateCurrentLocation(gesture)
}
}
}
}
public class LPRTableViewController: UITableViewController, LPRTableViewDelegate {
/** Returns the long press to reorder table view managed by the controller object. */
public var lprTableView: LPRTableView! { return tableView as! LPRTableView }
public override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
initialize()
}
public override init(style: UITableViewStyle) {
super.init(style: style)
initialize()
}
public required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initialize()
}
private func initialize() {
tableView = LPRTableView()
tableView.dataSource = self
tableView.delegate = self
registerClasses()
lprTableView.longPressReorderDelegate = self
}
/** Override this method to register custom UITableViewCell subclass(es). DO NOT call `super` within this method. */
public func registerClasses() {
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}
/** Provides the delegate a chance to modify the cell visually before dragging occurs. Defaults to using the cell as-is if not implemented. The default implementation of this method is empty—no need to call `super`. */
public func tableView(tableView: UITableView!, draggingCell cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Empty implementation, just to simplify overriding (and to show up in code completion).
return cell
}
/** Called within an animation block when the dragging view is about to show. The default implementation of this method is empty—no need to call `super`. */
public func tableView(tableView: UITableView!, showDraggingView view: UIView, atIndexPath indexPath: NSIndexPath) {
// Empty implementation, just to simplify overriding (and to show up in code completion).
}
/** Called within an animation block when the dragging view is about to hide. The default implementation of this method is empty—no need to call `super`. */
public func tableView(tableView: UITableView!, hideDraggingView view: UIView, atIndexPath indexPath: NSIndexPath) {
// Empty implementation, just to simplify overriding (and to show up in code completion).
}
}