【问题标题】:Stop UITableViewCells from animating when animating a UITableView动画 UITableView 时停止 UITableViewCells 动画
【发布时间】:2013-04-29 17:34:37
【问题描述】:

我正在为 UITableView 制作动画。我希望表格视图像这样滑动打开:

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

但是,它的打开方式是这样的:

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

注意表格单元格本身的动画效果。我怎样才能阻止这种情况发生?

我正在使用自动布局。代码如下:

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

其他细节,可能重要也可能不重要:

  • 表格视图的父级是滚动视图

  • 我已经重写了table view的intrinsicContentSize来返回table view中所有单元格的高度,所以table view不会滚动

  • 我正在更改表格视图的数据源,然后再显示它

【问题讨论】:

    标签: ios uitableview autolayout uianimation


    【解决方案1】:

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

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

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [UIView performWithoutAnimation:^{
            [cell layoutIfNeeded];
        }];
    }
    

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

        【解决方案3】:

        您是否考虑过用动画代替位于表格视图上方的不透明子视图?

        [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);
        }];
        

        【讨论】:

        • 我认为这不适用于我的情况,因为表格下方的子视图也需要与表格一起上下动画。
        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-15
          • 1970-01-01
          • 2021-12-31
          • 1970-01-01
          相关资源
          最近更新 更多