在UITableView中,我如何知道某行的scrollRectToVisible何时完成?

jos*_*ine 10 animation objective-c uitableview ios

当我在UITableView中选择一行时,我正在调用scrollRectToVisible:animated行的帧的GCRect,然后立即做一些其他动画.我的问题是我不知道动画何时scrollRectToVisible:animated完成.

我的代码:

- (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! 
}
Run Code Online (Sandbox Code Playgroud)

ott*_*tto 16

我跑过这个UIScrollViewDelegate方法:

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
   // Do your stuff.
}
Run Code Online (Sandbox Code Playgroud)

只调用动画卷轴.不要求基于触摸的滚动.似乎工作得很好.

  • 很好,但是请注意,rect已经是可见的,将不会调用此方法 (2认同)

Luk*_*ski 7

更简单的方法是将滚动代码封装在UIView animateWith [...]块中,如下所示:

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

请注意,scrollRectToVisible:animated:方法中的动画== NO.


Ser*_*nov 4

该协议UITableViewDelegate符合UIScrollViewDelegate. 您可以在手动滚动时设置BOOL参数,然后将其签入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
    }

}
Run Code Online (Sandbox Code Playgroud)

别忘了设置UITableViewDelegate.