【问题标题】:UITableViewCell animation when scrollingUITableViewCell 滚动时的动画
【发布时间】:2010-11-25 23:40:13
【问题描述】:

我已经用 Love 的 example for Multi-row selection 实现了 Cocoa,这涉及创建一个自定义 UITableViewCell,它在 layoutSubviews 中启动动画以在每行左侧显示复选框,如下所示:

- (void)layoutSubviews
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationBeginsFromCurrentState:YES];

    [super layoutSubviews];

    if (((UITableView *)self.superview).isEditing)
    {
        CGRect contentFrame = self.contentView.frame;
        contentFrame.origin.x = EDITING_HORIZONTAL_OFFSET;
        self.contentView.frame = contentFrame;
    }
    else
    {
        CGRect contentFrame = self.contentView.frame;
        contentFrame.origin.x = 0;
        self.contentView.frame = contentFrame;
    }

    [UIView commitAnimations];
}

这很好用,并且对于所有意图和目的,我的 UITableView 都可以正常工作。但是我遇到了一个小的美学问题:滚动之前未显示的 UITableView 行时将启动它们的滑动动画,这意味着动画在某些行进入视图时是交错的。

这是可以理解的,因为 setAnimationBeginsFromCurrentState 已设置为 YES,并且 UITableView 中更下方的行尚未更新其帧位置。为了解决这个问题,我尝试使用 willDisplayCell 覆盖在 UITableView 处于编辑模式时变为可见的单元格的动画。基本上绕过动画并立即更新行框架,以使其看起来好像单元格已经动画到位,如下所示:

/*
 Since we animate the editing transitions, we need to ensure that all animations are cancelled
 when a cell is scheduled to appear, so that things happen instantly.
 */
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    [cell.contentView.layer removeAllAnimations];   

    if(tableView.isEditing) {
        CGRect contentFrame = cell.contentView.frame;
        contentFrame.origin.x = EDITING_HORIZONTAL_OFFSET;
        cell.contentView.frame = contentFrame;
    } else {
        CGRect contentFrame = cell.contentView.frame;
        contentFrame.origin.x = 0;
        cell.contentView.frame = contentFrame;
    }
}

不幸的是,这似乎没有任何效果。有谁知道我该如何解决这个问题?

【问题讨论】:

    标签: iphone uitableview multirow


    【解决方案1】:

    不确定您是否仍然需要这个问题的答案,但我刚刚遇到了完全相同的问题,所以我想我会分享我的解决方案。我以与您提到的 Cocoa with Love 博客文章中描述的相同方式实现了 Multi-Selection。

    在 cellAtIndexPath DataSource 方法中,当我创建一个新单元格时(如果该单元格已经在可重用单元格队列中),我检查 tableView 是否处于编辑模式,如果是,我在单元格上设置了一个属性(我创建了我自己的具有 EnableAnimation 属性的自定义单元格)为 false,因此当它获取 SetEditing 回调时,它不会为单元格设置动画,而只会设置框架。在 Cell 类的构造函数中,我将 EnableAnimation 设置为 true,当调用 SetEditing 回调时,我将 EnableAnimation 设置为传入的 animate 参数。我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      相关资源
      最近更新 更多