I think I came up with a better solution to this problem using the latest GestureRecognisers. I use the following LongPress gesture recognizer inside my base TableView controller.
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{ if([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]){ [gestureRecognizer addTarget:self action:@selector(longGestureAction:)]; } return YES; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; } -(void)longGestureAction:(UILongPressGestureRecognizer *)gesture{ UITableViewCell *cell= (UITableViewCell *)[gesture view]; switch ([gesture state]) { case UIGestureRecognizerStateBegan:{ NSIndexPath *ip = [self.tableView indexPathForCell:cell]; [self.tableView setScrollEnabled:NO]; if(ip!=nil){ [self.draggableDelegate dragAndDropTableViewController:self draggingGestureWillBegin:gesture forCell:cell]; UIView *draggedView = [self.draggableDelegate dragAndDropTableViewControllerView:self ];
In the TableViews that extend this base class, I add the following to each cell in cellForIndexPath TableViewDataSource.
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:cell action:nil]; longPress.delegate = self; [cell addGestureRecognizer:longPress];
and finally, all you have to do is implement delegate methods, like
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{ if([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]){ [gestureRecognizer addTarget:self action:@selector(longGestureAction:)]; } return YES; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; } -(void)longGestureAction:(UILongPressGestureRecognizer *)gesture{ UITableViewCell *cell= (UITableViewCell *)[gesture view]; switch ([gesture state]) { case UIGestureRecognizerStateBegan:{ NSIndexPath *ip = [self.tableView indexPathForCell:cell]; [self.tableView setScrollEnabled:NO]; if(ip!=nil){ [self.draggableDelegate dragAndDropTableViewController:self draggingGestureWillBegin:gesture forCell:cell]; UIView *draggedView = [self.draggableDelegate dragAndDropTableViewControllerView:self ];
Here you can find the source here . It took me a long time to get this right, so I really hope this saves someone else.
source share