Skip to content

Commit f21d9fa

Browse files
Ensured gesture recognizer cancels before begin.
1 parent 7f2ee8b commit f21d9fa

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

LPRTableView.swift

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ open class LPRTableView: UITableView {
8989

9090
fileprivate func initialize() {
9191
longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(LPRTableView._longPress(_:)))
92+
longPressGestureRecognizer.delegate = self
9293
addGestureRecognizer(longPressGestureRecognizer)
9394

9495
estimatedRowHeight = 0.0
@@ -98,37 +99,35 @@ open class LPRTableView: UITableView {
9899

99100
}
100101

102+
extension LPRTableView: UIGestureRecognizerDelegate {
103+
104+
open override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
105+
let location: CGPoint = gestureRecognizer.location(in: self)
106+
let indexPath: IndexPath? = indexPathForRow(at: location)
107+
let sections: Int = numberOfSections
108+
var rows: Int = 0
109+
for i in 0..<sections {
110+
rows += numberOfRows(inSection: i)
111+
}
112+
// Long press gesture should not begin if it was not on a valid row or our table is empty
113+
// or the `dataSource.tableView(_:canMoveRowAt:)` doesn't allow moving the row.
114+
return (rows > 0)
115+
&& (indexPath != nil)
116+
&& canMoveRowAt(indexPath: indexPath!)
117+
}
118+
119+
}
120+
101121
extension LPRTableView {
102122

103123
fileprivate func canMoveRowAt(indexPath: IndexPath) -> Bool {
104124
return (dataSource?.responds(to: #selector(UITableViewDataSource.tableView(_:canMoveRowAt:))) == false) || (dataSource?.tableView?(self, canMoveRowAt: indexPath) == true)
105125
}
106126

107-
fileprivate func cancelGesture() {
108-
longPressGestureRecognizer.isEnabled = false
109-
longPressGestureRecognizer.isEnabled = true
110-
}
111-
112127
@objc internal func _longPress(_ gesture: UILongPressGestureRecognizer) {
113128
let location: CGPoint = gesture.location(in: self)
114129
let indexPath: IndexPath? = indexPathForRow(at: location)
115130

116-
let sections: Int = numberOfSections
117-
var rows: Int = 0
118-
for i in 0..<sections {
119-
rows += numberOfRows(inSection: i)
120-
}
121-
122-
// Get out of here if the long press was not on a valid row or our table is empty
123-
// or the dataSource tableView:canMoveRowAtIndexPath: doesn't allow moving the row.
124-
if (rows == 0) ||
125-
((gesture.state == .began) && (indexPath == nil)) ||
126-
((gesture.state == .ended) && (currentLocationIndexPath == nil)) ||
127-
((gesture.state == .began) && !canMoveRowAt(indexPath: indexPath!)) {
128-
cancelGesture()
129-
return
130-
}
131-
132131
switch gesture.state {
133132
case .began: // Started
134133
hapticFeedbackSetup()
@@ -228,7 +227,7 @@ extension LPRTableView {
228227
} else {
229228
scrollRate = 0.0
230229
}
231-
case .ended, // Dropped
230+
case .ended where currentLocationIndexPath != nil, // Dropped
232231
.cancelled,
233232
.failed:
234233
// Remove previously cached Gesture location

0 commit comments

Comments
 (0)