【问题标题】:Animation finishes immediately, regardless of duration and delay动画立即结束,无论持续时间和延迟如何
【发布时间】:2016-09-13 05:24:19
【问题描述】:

我有这个动画块,但是cell的背景色立即是clear

cell.mylabel.backgroundColor = .orange
UIView.animate(withDuration: 3.0, delay:3, animations: {
    cell.mylabel.backgroundColor = .clear
})

关键是我在 cellForRowAt 中运行它

我尝试将动画移动到我的自定义单元格,但再次失败:

override func didMoveToWindow() {
            UIView.animate(withDuration: 3.0, delay:3, animations: {
                self.mylabel.backgroundColor = .clear
            })
    }

【问题讨论】:

  • tableView:willDisplayCell:forRowAtIndexPath:而不是tableView:cellForRowAtIndexPath中做动画

标签: ios swift animation


【解决方案1】:

关键是我在 cellForRowAt 中运行它

这还为时过早,因为单元格可能尚未在屏幕视图层次结构中。您只能为屏幕视图层次结构中的视图设置动画。在将单元格添加为表格视图的子视图后,您需要找到一种添加动画的方法。

我会创建一个自定义的UITableViewCell 子类。在子类中,我将覆盖didMoveToWindow 以将动画添加到self

或者,它可以覆盖表视图控制器中的viewDidLayoutSubviews(如果有的话),并在该方法中添加动画。

【讨论】:

  • 使用tableView:willDisplayCell:forRowAtIndexPath: 也可以。
  • tableView:willDisplayCell:forRowAtIndexPath 也不起作用,:(
【解决方案2】:

这样试试,

cell.mylabel.layer.backgroundColor = UIColor.redColor().CGColor
UIView.animate(withDuration: 3.0, delay:3, animations: {
    cell.mylabel.layer.backgroundColor = UIColor.clearColor().CGColor
})

【讨论】:

  • 为什么要改成图层?
  • 我没有找到查看背景颜色的动画。是的,当然我们可以使用 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { ((RouteListCell *)cell).name.backgroundColor = [UIColor clearColor]; [((RouteListCell *)cell).name backgroundGlowAnimationFromColor:[UIColor whiteColor] toColor:[UIColor redColor] clearAnimationsFirst:YES]; }
  • @AVEbrahimi 我从stackoverflow.com/questions/3762561/…找到了这个答案
  • @Sujit 将来,当您复制别人的答案时,您应该在答案中包含正确的参考。否则就是抄袭。更好的是,如果您认为另一个问题是重复的,请投票以关闭该问题作为重复。
  • @rmaddy 谢谢哥们。我会记得
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-05
相关资源
最近更新 更多