forked from SwipeCellKit/SwipeCellKit
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSwipeTableViewCell.swift
executable file
·231 lines (178 loc) · 8.11 KB
/
SwipeTableViewCell.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
//
// SwipeTableViewCell.swift
//
// Created by Jeremy Koch
// Copyright © 2017 Jeremy Koch. All rights reserved.
//
import UIKit
/**
The `SwipeTableViewCell` class extends `UITableViewCell` and provides more flexible options for cell swiping behavior.
The default behavior closely matches the stock Mail.app. If you want to customize the transition style (ie. how the action buttons are exposed), or the expansion style (the behavior when the row is swiped passes a defined threshold), you can return the appropriately configured `SwipeOptions` via the `SwipeTableViewCellDelegate` delegate.
*/
open class SwipeTableViewCell: UITableViewCell {
/// The object that acts as the delegate of the `SwipeTableViewCell`.
public weak var delegate: SwipeTableViewCellDelegate?
var state = SwipeState.center
var actionsView: SwipeActionsView?
var scrollView: UIScrollView? {
return tableView
}
var indexPath: IndexPath? {
return tableView?.indexPath(for: self)
}
var panGestureRecognizer: UIGestureRecognizer
{
return swipeController.panGestureRecognizer;
}
var swipeController: SwipeController!
var isPreviouslySelected = false
weak var tableView: UITableView?
/// :nodoc:
open override var frame: CGRect {
set { super.frame = state.isActive ? CGRect(origin: CGPoint(x: frame.minX, y: newValue.minY), size: newValue.size) : newValue }
get { return super.frame }
}
/// :nodoc:
open override var layoutMargins: UIEdgeInsets {
get {
return frame.origin.x != 0 ? swipeController.originalLayoutMargins : super.layoutMargins
}
set {
super.layoutMargins = newValue
}
}
/// :nodoc:
override public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
configure()
}
/// :nodoc:
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configure()
}
deinit {
tableView?.panGestureRecognizer.removeTarget(self, action: nil)
}
func configure() {
clipsToBounds = false
swipeController = SwipeController(swipeable: self, actionsContainerView: self)
swipeController.delegate = self
}
/// :nodoc:
override open func prepareForReuse() {
super.prepareForReuse()
reset()
resetSelectedState()
}
/// :nodoc:
override open func didMoveToSuperview() {
super.didMoveToSuperview()
var view: UIView = self
while let superview = view.superview {
view = superview
if let tableView = view as? UITableView {
self.tableView = tableView
swipeController.scrollView = tableView;
tableView.panGestureRecognizer.removeTarget(self, action: nil)
tableView.panGestureRecognizer.addTarget(self, action: #selector(handleTablePan(gesture:)))
return
}
}
}
/// :nodoc:
override open func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
if editing {
hideSwipe(animated: false)
}
}
// Override so we can accept touches anywhere within the cell's minY/maxY.
// This is required to detect touches on the `SwipeActionsView` sitting alongside the
// `SwipeTableCell`.
/// :nodoc:
override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
guard let superview = superview else { return false }
let point = convert(point, to: superview)
if !UIAccessibility.isVoiceOverRunning {
for cell in tableView?.swipeCells ?? [] {
if (cell.state == .left || cell.state == .right) && !cell.contains(point: point) {
tableView?.hideSwipeCell()
return false
}
}
}
return contains(point: point)
}
func contains(point: CGPoint) -> Bool {
return point.y > frame.minY && point.y < frame.maxY
}
/// :nodoc:
override open func setHighlighted(_ highlighted: Bool, animated: Bool) {
if state == .center {
super.setHighlighted(highlighted, animated: animated)
}
}
/// :nodoc:
override open func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return swipeController.gestureRecognizerShouldBegin(gestureRecognizer)
}
/// :nodoc:
open override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
swipeController.traitCollectionDidChange(from: previousTraitCollection, to: self.traitCollection)
}
@objc func handleTablePan(gesture: UIPanGestureRecognizer) {
if gesture.state == .began {
hideSwipe(animated: true)
}
}
func reset() {
swipeController.reset()
clipsToBounds = false
}
func resetSelectedState() {
if isPreviouslySelected {
if let tableView = tableView, let indexPath = tableView.indexPath(for: self) {
tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
}
}
isPreviouslySelected = false
}
}
extension SwipeTableViewCell: SwipeControllerDelegate {
func swipeController(_ controller: SwipeController, canBeginEditingSwipeableFor orientation: SwipeActionsOrientation) -> Bool {
return self.isEditing == false
}
func swipeController(_ controller: SwipeController, editActionsForSwipeableFor orientation: SwipeActionsOrientation) -> [SwipeAction]? {
guard let tableView = tableView, let indexPath = tableView.indexPath(for: self) else { return nil }
return delegate?.tableView(tableView, editActionsForRowAt: indexPath, for: orientation)
}
func swipeController(_ controller: SwipeController, editActionsOptionsForSwipeableFor orientation: SwipeActionsOrientation) -> SwipeOptions {
guard let tableView = tableView, let indexPath = tableView.indexPath(for: self) else { return SwipeOptions() }
return delegate?.tableView(tableView, editActionsOptionsForRowAt: indexPath, for: orientation) ?? SwipeOptions()
}
func swipeController(_ controller: SwipeController, visibleRectFor scrollView: UIScrollView) -> CGRect? {
guard let tableView = tableView else { return nil }
return delegate?.visibleRect(for: tableView)
}
func swipeController(_ controller: SwipeController, willBeginEditingSwipeableFor orientation: SwipeActionsOrientation) {
guard let tableView = tableView, let indexPath = tableView.indexPath(for: self) else { return }
// Remove highlight and deselect any selected cells
super.setHighlighted(false, animated: false)
isPreviouslySelected = isSelected
tableView.deselectRow(at: indexPath, animated: false)
delegate?.tableView(tableView, willBeginEditingRowAt: indexPath, for: orientation)
}
func swipeController(_ controller: SwipeController, didEndEditingSwipeableFor orientation: SwipeActionsOrientation) {
guard let tableView = tableView, let indexPath = tableView.indexPath(for: self), let actionsView = self.actionsView else { return }
resetSelectedState()
delegate?.tableView(tableView, didEndEditingRowAt: indexPath, for: actionsView.orientation)
}
func swipeController(_ controller: SwipeController, didDeleteSwipeableAt indexPath: IndexPath) {
tableView?.deleteRows(at: [indexPath], with: .none)
}
func swipeCellShouldAllowOtherGestureRecoginzerToWorkSimultaneously(_ otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return delegate?.shouldRecognizeOtherGestureRecoginzerSimultaneously(otherGestureRecognizer) ?? false
}
}