简体   繁体   中英

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

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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM