tomiQrsd
tomiQrsd

Reputation: 119

UITableView insertRowsAtIndexPaths blocks UIPanGestureRecognizer

Intro

I have UIPanGestureRecognizercreated like this:

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setCancelsTouchesInView:NO];
[self.view addGestureRecognizer:panRecognizer];

and tableView with NSTimer that fires every 0.3 s to insert new row to tableView if new data appears in database.

Creation of NSTimer :

NSTimer* timer = [NSTimer timerWithTimeInterval:0.3 target:self selector:@selector(onTick:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

Inside onTick selector if new data appeared I perform simple row insertion:

    [_tableView beginUpdates];
    [_tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
    [_tableView endUpdates];

The tableView has disabled scrolling and user interaction.

Issue

Calling [_tableView insertRowsAtIndexPaths:]makes UIPanGesureRecognizer unable to register any new touch event (panning and tapping). If I start to pan and then begin to add rows the panning doesn't break. I just can't start panning while rows are being added.

Things I tried and facts

Does anyone know why it happens and how to fix it?

Upvotes: 0

Views: 84

Answers (1)

Vlad Fedoseev
Vlad Fedoseev

Reputation: 152

Implement the gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer delegate method which always returns YES to allow the tableView's internal UIPanGestureRecognizer work simultaneously with your panRecognizer:

panRecognizer.delegate = self;

Then

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

I also noticed that insertRowsAtIndexPath using animation takes approx. 1/3 second to complete but you call it every 0.3 sec. It may cause some problems as well. Try to update rows ONLY if there's new data to add to the table view:

- (void)onTick() {
    //your code

    if (shouldUpdate) {
        //do the updates only if there's new data to add to the table view
    }
}

Upvotes: 0

Related Questions