@@ -89,6 +89,7 @@ open class LPRTableView: UITableView {
89
89
90
90
fileprivate func initialize( ) {
91
91
longPressGestureRecognizer = UILongPressGestureRecognizer ( target: self , action: #selector( LPRTableView . _longPress ( _: ) ) )
92
+ longPressGestureRecognizer. delegate = self
92
93
addGestureRecognizer ( longPressGestureRecognizer)
93
94
94
95
estimatedRowHeight = 0.0
@@ -98,37 +99,35 @@ open class LPRTableView: UITableView {
98
99
99
100
}
100
101
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
+
101
121
extension LPRTableView {
102
122
103
123
fileprivate func canMoveRowAt( indexPath: IndexPath ) -> Bool {
104
124
return ( dataSource? . responds ( to: #selector( UITableViewDataSource . tableView ( _: canMoveRowAt: ) ) ) == false ) || ( dataSource? . tableView ? ( self , canMoveRowAt: indexPath) == true )
105
125
}
106
126
107
- fileprivate func cancelGesture( ) {
108
- longPressGestureRecognizer. isEnabled = false
109
- longPressGestureRecognizer. isEnabled = true
110
- }
111
-
112
127
@objc internal func _longPress( _ gesture: UILongPressGestureRecognizer ) {
113
128
let location : CGPoint = gesture. location ( in: self )
114
129
let indexPath : IndexPath ? = indexPathForRow ( at: location)
115
130
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
-
132
131
switch gesture. state {
133
132
case . began: // Started
134
133
hapticFeedbackSetup ( )
@@ -228,7 +227,7 @@ extension LPRTableView {
228
227
} else {
229
228
scrollRate = 0.0
230
229
}
231
- case . ended, // Dropped
230
+ case . ended where currentLocationIndexPath != nil , // Dropped
232
231
. cancelled,
233
232
. failed:
234
233
// Remove previously cached Gesture location
0 commit comments