Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Swipe to Delete on UITableView to Work With UIPanGestureRecognizer

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

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

Within the main view I have a UITableView which has this code to enable the swipe to delete feature:

- (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 to the log and the Delete button does not show up. I believe the reason for this is the UIPanGestureRecognizer, but I am not sure. If this is correct how should I go about fixing this. If this is not correct please provide the cause and fix. Thanks.

like image 632
carloabelli Avatar asked Aug 22 '13 00:08

carloabelli


1 Answers

From the document:

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

Your UIPanGestureRecognizer recognizes the swipe gesture first, so your UITableView does not receive touches anymore.

To make the table view receives touch simultaneously with the gesture recognizer, add this to the gesture recognizer's delegate:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}
like image 163
honganhkhoa Avatar answered Oct 19 '22 07:10

honganhkhoa