替换相似单元格时平滑表动画

时间:2014-08-16 05:20:06

标签: ios uitableview uiviewanimation

我想用另一个类似内容的单元格替换表格单元格动画

e.g。一个单元格中央对齐标签,其他较大单元格,其他内容在左上角有相同的标签(文本,字体和颜色相同)。知道如何实现吗?

我尝试过:

  1. 具有reloadSections:reloadRowsAtIndexPath:功能的标准表格单元格动画。但我无法实现平滑效果,即中心对齐标签应移动到左上角动画,同时更换单元格

  2. [self.tableView beginUpdates]; [self.tableView endUpdates];

    只给我换身动画。

3 个答案:

答案 0 :(得分:1)

tableView:willDisplayCell:forRowAtIndexPath:委托方法中,您可以使用cellForRowAtIndexPath:方法检索当前显示(即旧)单元格。
使用此功能,您可以使用reloadRowsAtIndexPaths:withRowAnimation:方法执行此操作 你要做的是:

  1. 使用旧单元格匹配新单元格的标签位置
  2. 将两个单元格的标签设置为新位置
  3. 像:

    - (void)tableView:(UITableView *)tableView willDisplayCell:(BaseCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        BaseCell *oldCell = (BaseCell *)[self.tableView cellForRowAtIndexPath:indexPath];
        if(oldCell != nil && oldCell != cell) {
            CGRect oldFrame = oldCell.titleLabel.frame;
            CGRect newFrame = cell.titleLabel.frame;
    
            cell.titleLabel.frame = (CGRect){oldFrame.origin, newFrame.size};
            [UIView animateWithDuration:0.25 animations:^{
                oldCell.titleLabel.frame = (CGRect){newFrame.origin, oldFrame.size};
                cell.titleLabel.frame = (CGRect){newFrame.origin, newFrame.size};
            } completion:^(BOOL finished) {
                // reset oldCell's titleLabel position for reuse
                oldCell.titleLabel.frame = oldFrame;
            }];
        }
    }
    

    您需要关注的一些事项:

    • 不要将autolayout用于单元格笔尖。这将使事情变得太复杂。
    • 请务必重置标签的位置以便重复使用。
    • UITableViewRowAnimationFade用于reloadRowsAtIndexPaths:withRowAnimation:

答案 1 :(得分:0)

表格单元格只有一些预定义的可能动画。

您应该通过更改[UIView animateWithDuration:animations:]内单元格内的框架来手动设置标签动画。就像你动画任何其他视图一样。

tableView:cellForRowAtIndexPath:重新使用/重新配置单元格时,您还必须跟踪标签的状态。

答案 2 :(得分:0)

您可以使用

在单元格上执行动画
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

委托方法。例如,您可以使用UIView动画或CABasicAnimation这样淡入细胞:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    [cell setAlpha:0.1];
    [UIView animateWithDuration:0.5f animations:^{
        [cell setAlpha:1];
    }];
}