制作tableView Cells一个接一个地落到位(uitableViewCell动画)

Making tableView Cells Fall into place one after the other (tableViewCellAnimation)

我是 obj-C 的新手,我有这段代码可以让单元格从页面右侧滑入(当我在重新加载 table 数据后调用它时)。

不是逐一检查,等待延迟然后进行下一步,而是在延迟计数后同时滑入。

为什么他们一次全部滑入而不是错开动画?

-(void) animate {
  for (UITableViewCell * aCell in [self.tableView visibleCells]) {
    [aCell setFrame: CGRectMake(320, aCell.frame.origin.y, aCell.frame.size.width, aCell.frame.size.height)];
    [UIView animateWithDuration: 0.5 delay: 0.5 options: UIViewAnimationOptionBeginFromCurrentState animations: ^ {
      [aCell setFrame: CGRectMake(0, aCell.frame.origin.y, aCell.frame.size.width, aCell.frame.size.height)];
    }
    completion: nil
    ];

  }

我认为您只是不了解延迟和阻塞的工作原理。让我尝试清除此代码:

  1. 你去扔所有[self.tableView visibleCells]数组
  2. 你调用[UIView animateWithDuration:...]方法的次数,visibleCells数组中有多少元素(延迟不会冻结你的代码)
  3. 在上面的每个方法中,都会在另一个步骤中启动 0.5 计时器。
  4. 0.5秒后动画块调用次数,visibleCells有多少元素

对于您来说,最简单的解决方案是将可见单元格保存在任何数组中,然后使用参数调用动画方法 - 您将在此方法中设置动画的单元格索引。在完成块中调用下一个动画。这是代码示例:

- (void)animate:(NSInteger)index {
    if (visibaleCells.count <= index) {
        return;
    }

    [UIView animateWithDuration:0.5 delay:0.5 options:0 animations:^{
        [visibaleCells[index] setFrame:CGRectMake(0, aCell.frame.origin.y, aCell.frame.size.width, aCell.frame.size.height)];
    } completion:^(BOOL finished) {
        [self animate:index + 1];
    }];
}