0

Looking to select multiple UITableviewCells by long pressing and running my finger down to subsequent cells and selecting those also,

Anyone know of a way of doing so with UILongPressGestureRecognizer

(think Spotify touch preview)

Abubakr Dar
  • 4,078
  • 4
  • 22
  • 28
patrickjquinn
  • 288
  • 1
  • 15

1 Answers1

2

This could get tricky, but maybe I can point you down the right path.

First, you need a reliable way to start a long press and keep tracking the pan. You will probably need to use UIPanGestureRecognizer in combination with UILongPressGestureRecognizer, applied to the view itself (not the cells). When the pan recognizer has activated, you'll need to disable the UITableView gesture recognizers so they don't interfere. All of this will require UIGestureRecognizerDelegate.

See this post for some more info on combining long press & pan recognizers:

Combining a UILongPressGestureRecognizer with a UIPanGestureRecognizer

Next you'll need an efficient way of hit testing for cells in the view. Assuming you're using self.view for the gesture recognizers, try:

if (panRecognizer.state == UIGestureRecognizerStateBegan || sender.state == UIGestureRecognizerStateChanged) {
    CGPoint location = [panRecognizer locationInView:self.view];
    UIView *subview = [self.view hitTest:location withEvent:nil];
}

Then you can associate it with a cell in your table view and trigger the selection:

if ([subview isKindOfClass:[UITableViewCell class]]) {
    NSIndexPath *path = [tableView indexPathForCell:(UITableViewCell *)subview];
    [tableView selectRowAtIndexPath:path animated:YES scrollPosition:UITableViewScrollPositionNone];
}

This is all purely hypothetical, but it should be a straightforward way of approaching this problem. The challenge will be configuring the gesture recognizers to work together with your table view.

Let me know if you have any questions!

Community
  • 1
  • 1
TTillage
  • 1,886
  • 2
  • 12
  • 10
  • Hi TTillage, i married the code specified above with that found in the linked post and while i can get the pan gesture delegate to fire after a longhold, it only fires on one cell and doesn't transfer over to the next. – patrickjquinn Feb 16 '15 at 22:45
  • If you want to post the relevant code snippets I'd be happy to take and look and see if anything seems out of place. – TTillage Feb 17 '15 at 20:06