8

I am animating a UITableView. I want the table view to slide open like this:

http://www.youtube.com/watch?v=XIGJLbiRsXE

However, it opens like this:

http://www.youtube.com/watch?v=UJUX-ILCB_0

Notice how the table cells themselves are animating. How can I stop this from happening?

I am using autolayout. The code is thus:

[theView addSubview:theTable];
[theTable reloadData];
[theTable invalidateIntrinsicContentSize];
[UIView animateWithDuration:.33 animations:^{
    [theView layoutIfNeeded];
 }];

Other details, which may or may not matter:

  • The table view's parent is a scrollView

  • I have overridden the intrinsicContentSize of the table view to return the height of all the cells in the table view, so the table view will not scroll

  • I am changing the datasource of the table view before displaying it

4

4 回答 4

13

我遇到了类似的问题,并且能够使用 UIViewperformWithoutAnimation:方法停止 UITableViewCell 上的动画。

使用tableView:willDisplayCell:forRowAtIndexPath:UITableViewDelegate 方法在显示之前获取对单元格的引用。然后,在performWithoutAnimation:块中,调用layoutIfNeeded单元格对象,让它有机会布局其子视图:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [UIView performWithoutAnimation:^{
        [cell layoutIfNeeded];
    }];
}
于 2014-10-19T20:00:06.053 回答
2

回答这个问题可能为时已晚,但这种情况下的问题通常在于动画块捕获了在下一个运行循环中安排的所有未决布局。

我使用的解决方案是。我在动画之前(在设置或使任何约束无效之前)调用 layoutIfNeeded,然后在动画块内调用。

在你的情况下,它会是这样的,

[theView addSubview:theTable];
[theTable reloadData];
[theView layoutIfNeeded];
[theTable invalidateIntrinsicContentSize];
[UIView animateWithDuration:.33 animations:^{
    [theView layoutIfNeeded];
 }];
于 2016-01-02T14:00:36.257 回答
0

在调用动画块内的 [theView layoutIfNeeded] 之前,先尝试布置表格。理想情况下,您可以分段布局,在这 0.33 秒内,您可能会在表格上看到一个滚动条,但目标是在动画块之外获得表格单元格布局更改。

于 2013-04-30T01:47:16.997 回答
0

您是否考虑过为位于表格视图上方的不透明子视图设置动画?

[theView addSubview:theTable];
[theTable reloadData];

UIView *subview = [[UIView alloc] initWithFrame:theTable.frame];
subview.backgroundColor = [UIColor whiteColor];
[theView addSubview:subview];

[UIView animateWithDuration:0.33 animations:^{
    subview.frame = CGRectMake(subview.frame.origin.x, subview.frame.size.height, subview.frame.size.width, 0.0);
}];
于 2013-04-29T19:20:55.550 回答