Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a UITableView, how do I know when scrollRectToVisible is complete for a row?

When I select a row in a UITableView, I'm calling scrollRectToVisible:animated on the GCRect of the row's frame, and immediately afterwards doing some other animations. My problem is that I don't know when the animation from scrollRectToVisible:animated is complete.

My code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRwoAtIndexPath:indexPath];

    [self.tableView scrollRectToVisible:cell.frame animated:YES];

    //more animations here, which I'd like to start only after the previous line is finished! 
}
like image 393
joseph.hainline Avatar asked Mar 29 '12 16:03

joseph.hainline


3 Answers

I ran across this UIScrollViewDelegate method:

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
   // Do your stuff.
}

Only called for animated scrolls. Not called for touch-based scrolls. Seems to work great.

like image 139
otto Avatar answered Nov 10 '22 05:11

otto


An easier way is to encapsulate the scrolling code in a UIView animateWith[...] block, like this:

[UIView animateWithDuration:0.3f animations:^{
    [self.tableView scrollRectToVisible:cell.frame animated:NO];
} completion:^(BOOL finished) {
    // Some completion code
}];

Note that animated == NO in the scrollRectToVisible:animated: method.

like image 30
Lukas Kalinski Avatar answered Nov 10 '22 06:11

Lukas Kalinski


The protocol UITableViewDelegate conforms to UIScrollViewDelegate. You can set BOOL parameter when you scroll manually and than check it in scrollViewDidScroll:

BOOL manualScroll;
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRwoAtIndexPath:indexPath];

    manualScroll = YES;
    [self.tableView scrollRectToVisible:cell.frame animated:YES]; 
}
...
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (manualScroll)
    {
        manualScroll = NO;
        //Do your staff
    }

}

Don't forget to set UITableViewDelegate.

like image 45
Sergey Kuryanov Avatar answered Nov 10 '22 06:11

Sergey Kuryanov