Get Swipe to delete in UITableView to work with UIPanGestureRecognizer

I have a UIPanGuestureRecognizer added to the whole view using this code:

UIPanGestureRecognizer *pgr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)]; [[self view] addGestureRecognizer:pgr]; 

In the main view, I have a UITableView that has this code to enable the swipe removal function:

 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"RUNNING2"); return UITableViewCellEditingStyleDelete; } - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row >= _firstEditableCell && _firstEditableCell != -1) NSLog(@"RUNNING1"); return YES; else return NO; } 

Only RUNNING1 is printed in the RUNNING1 , and the Delete button is not displayed. I believe the reason for this is UIPanGestureRecognizer, but I'm not sure. If this is correct, how should I fix it. If this is not the case, indicate the cause and repair. Thank you

+7
ios objective-c uitableview uipangesturerecognizer
source share
3 answers

From document :

If the gesture recognizer recognizes its gesture, the remaining touches for the view are canceled.

Your UIPanGestureRecognizer first recognizes with a swipe gesture, so your UITableView no longer receives touches.

To make the table view get touched simultaneously with the recognizing gesture, add this to the gesture recognizer delegate:

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } 
+14
source share

If you use UIPanGuestureRecognizer, for example, to show the side menu, you can see some unwanted side effects when you simply return YES in all cases, as suggested in the accepted answer. For example, the side menu opens when scrolling up / down in the table view (with an additional very small left / right direction) or the delete button, which is strange when you open the side menu. What you can do to prevent these side effects is to allow only simultaneous horizontal gestures. This will make the delete button work correctly, but at the same time, other unwanted gestures will be blocked when moving the menu.

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { if ([otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) { UIPanGestureRecognizer *panGesture = (UIPanGestureRecognizer *)otherGestureRecognizer; CGPoint velocity = [panGesture velocityInView:panGesture.view]; if (ABS(velocity.x) > ABS(velocity.y)) return YES; } return NO; } 

or in Swift:

 func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { guard let panRecognizer = otherGestureRecognizer as? UIPanGestureRecognizer else { return false } let velocity = panRecognizer.velocity(in: panRecognizer.view) if (abs(velocity.x) > abs(velocity.y)) { return true } return false } 
0
source share

If the accepted answer does not work. Try to add

 panGestureRecognizer.cancelsTouchesInView = false 

Make sure you do not add a gesture to the table view directly. I added a pan gesture in the ViewController view and I can confirm that it works.

0
source share

All Articles