UITableView ReloadData 动画单元格 UI
UITableView ReloadData Animate Cell UI
在我的表格视图中,我需要在表格视图重新加载其数据时为每个单元格设置动画 UI。
这需要自定义动画,而不仅仅是淡入淡出或默认过渡。
例如,当点击 'Edit' 按钮时,在我看来,tableview 会重新加载并且每个单元格都会更新其 UI 以适应这种新的编辑模式。
然而,在这次重新加载期间,我尝试使用 UIView
动画块来更新单元格中 UILabel
的约束,但它不会动画。
这是我在 tableview 上调用 reloadData
时 运行 的代码。这在 cellForRowAtIndexPath
方法中调用。
[UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^
{
//does not animate
_lblContainerLeftConstraint.constant = 18;
[self setNeedsLayout];
//does animate
_reorderIcon.alpha = 0.5f;
} completion:nil];
我认为您需要在 willDisplayCell
而不是 cellForRowAtIndexPath
中执行此操作,因为它是在显示之前调用的,它尽可能接近它实际出现在屏幕上的位置。
然后在动画块前设置新的约束值,同时调用[cell.contentView setNeedsLayout]
表示单元格内容需要布局。
最后只添加 [cell.contentView layoutIfNeeded]
和您在动画块中的 alpha 变化,它应该在您的时间段内动画约束变化。
在我的表格视图中,我需要在表格视图重新加载其数据时为每个单元格设置动画 UI。
这需要自定义动画,而不仅仅是淡入淡出或默认过渡。
例如,当点击 'Edit' 按钮时,在我看来,tableview 会重新加载并且每个单元格都会更新其 UI 以适应这种新的编辑模式。
然而,在这次重新加载期间,我尝试使用 UIView
动画块来更新单元格中 UILabel
的约束,但它不会动画。
这是我在 tableview 上调用 reloadData
时 运行 的代码。这在 cellForRowAtIndexPath
方法中调用。
[UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^
{
//does not animate
_lblContainerLeftConstraint.constant = 18;
[self setNeedsLayout];
//does animate
_reorderIcon.alpha = 0.5f;
} completion:nil];
我认为您需要在 willDisplayCell
而不是 cellForRowAtIndexPath
中执行此操作,因为它是在显示之前调用的,它尽可能接近它实际出现在屏幕上的位置。
然后在动画块前设置新的约束值,同时调用[cell.contentView setNeedsLayout]
表示单元格内容需要布局。
最后只添加 [cell.contentView layoutIfNeeded]
和您在动画块中的 alpha 变化,它应该在您的时间段内动画约束变化。