如何知道UITableView动画何时完成?

Boo*_*oon 11 iphone uikit

我想做一些事情来回应某些UITableView动画的结束.例如,我希望表视图单元格在通过scrollToRowAtIndexPath滚动到之后突出显示(通过selectRowAtIndexPath).

如何才能做到这一点?

小智 29

基本模板:

[UIView animateWithDuration:0.2 animations:^{
    //do some animations, call them with animated:NO
} completion:^(BOOL finished){
    //Do something after animations finished     
}];   
Run Code Online (Sandbox Code Playgroud)

示例:滚动到第100行.完成后,获取此行的单元格,并使用tag = 1的单元格内容视图到firstResponder:

[UIView animateWithDuration:0.2 animations:^{
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:100 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    } completion:^(BOOL finished){
        //Do something after scrollToRowAtIndexPath finished, e.g.:
        UITableViewCell *nextCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:100 inSection:0]];
        [[nextCell.contentView viewWithTag:1] becomeFirstResponder];        
    }];   
Run Code Online (Sandbox Code Playgroud)

  • 它猜测它不再适用于iOS 6,因为它改变了动画的时间.检查以下SO答案以获得更可靠的方法:http://stackoverflow.com/a/12516056/908621 (4认同)
  • 我不知道这个解决方案是如何在iOS 4/5上工作但现在在iOS 6.1.4上我注意到在scrollToRowAtIndexPath更改contentOffset之后,此位置的单元格不可见,因此对于一个分数其次,tableview完全为空白,然后绘制单元格并按预期显示键盘.我想UITableView并不是以这种方式使用,或者至少不是在这个版本的iOS中使用.使用setContentOffset而不是scrollToRowAtIndexPath会发生相同的结果. (2认同)

Bro*_*olf -3

好吧,如果您想在scrollToRowAtIndexPath 被触发后执行操作。

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
Run Code Online (Sandbox Code Playgroud)

您需要创建一个 CAAnimation 指针,例如

CAAnimation *myAnimation;
Run Code Online (Sandbox Code Playgroud)

然后将delgate设置为self

myAnimation.delegate = self;
Run Code Online (Sandbox Code Playgroud)

完成此操作后,将激活以下委托方法,您可以在其中放置代码:

- (void)animationDidStart:(CAAnimation *)theAnimation

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
Run Code Online (Sandbox Code Playgroud)