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)
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)
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!