使tableView Cells一个接一个地到位(uitableViewCell Animation)

时间:2015-02-19 22:21:20

标签: ios objective-c uitableview animation cell

嘿所以我有这个代码,目前使得单元格从页面右侧滑入(当我在重新加载表格数据后调用它),而不是通过它们中的每一个,等待延迟然后做下一步,他们都在延迟计数后同时滑入。

为什么会这样? 我几乎可以肯定这会起作用......

    - (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 个答案:

答案 0 :(得分:2)

我认为你只是不明白延迟和阻塞是如何工作的。让我试着清除这段代码:

  1. 你扔掉所有[self.tableView visibleCells]数组
  2. 您拨打[UIView animateWithDuration:...]方法次数,visibleCells数组中的元素数量(延迟不会冻结您的代码)
  3. 在上述每种方法中,在另一个步骤中启动0.5计时器。
  4. 0.5秒后动画块调用次数,visibleCells有多少元素
  5. 对于您来说,最简单的解决方案是在任何数组中保存可见单元格,然后使用参数调用动画方法 - 您将在此方法中设置动画的索引。在完成块中调用下一个动画。这是代码示例:

    - (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];
        }];
    }