OGeek|极客世界-中国程序员成长平台

标题: ios - 单元格出现时使用 AutoLayout 为 UITableViewCell 内容设置动画 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 03:16
标题: ios - 单元格出现时使用 AutoLayout 为 UITableViewCell 内容设置动画

我有一个 UITableView 显示单元格。我正在使用自动布局。在某些时候,我将单元格添加到我的 tableView 的一部分中,并且一旦单元格可见,我需要为每个单元格的 subview 设置动画。

我编写了以下方法来为单元格的内容设置动画:

- (void)animateWithPercentageNSNumber *)percentage
{
    self.progressViewWidthConstraint.constant = [percentage intValue];
    [self.progressView setNeedsUpdateConstraints];

    [UIView animateWithDuration:0.6f animations:^{
        [self.progressView layoutIfNeeded];
    }];
}

该方法按预期工作,但我不知道何时调用它。

如果我在调用 - (void)tableViewUITableView *)tableView willDisplayCellUITableViewCell *)cell forRowAtIndexPathNSIndexPath *)indexPath 时调用它,我最终会为整个布局设置动画我的单元格,因为单元格之前没有自己布局。

我也尝试在 layoutSubviews 中调用它,但动画被跳过了。我想现在做动画还为时过早。

知道我的方法应该在哪里调用吗?



Best Answer-推荐答案


我已经通过在自定义单元格类的 awakeFromNib 或 didMoveToSuperview 中调用 performSelector:withObject:afterDelay: 来获得这种动画。即使延迟为 0,它也可以工作,但如果你只使用 performSelector,它就不起作用。我注意到,使用这种方法,如果某些单元格不在屏幕上,它们在滚动之前不会更新,并且滚动动画结束。您可以在单元格在屏幕上滚动时立即开始动画,即使滚动动画仍在进行中,也可以使用 dispatch_async 代替。

-(void)awakeFromNib {
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:2 animations:^{
            self.rightCon.constant = 150;
            [self.contentView layoutIfNeeded];
        } completion:nil];
    });
}

关于ios - 单元格出现时使用 AutoLayout 为 UITableViewCell 内容设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26865529/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4