【问题标题】:Animations added to table cell not working添加到表格单元格的动画不起作用
【发布时间】:2021-09-11 15:36:14
【问题描述】:

我在滚动后为自定义表格单元格设置动画时遇到问题。我需要使用滚动动画滚动到特定索引,然后通过更改单元格背景突出显示单元格。

我使用滚动

self.tableView.scrollToRow(at: index_path, at: position, animated: true)

在 willDisplayCell 回调中,我将动画添加到我使用 CAAnimation 滚动到的单元格中。

    let animation = CABasicAnimation(keyPath: "backgroundColor")
    animation.fromValue = UIColor.white.cgColor
    animation.toValue = UIColor.blue.withAlphaComponent(0.4).cgColor
    animation.duration = 1
    animation.autoreverses = true
    animation.isRemovedOnCompletion = true
    cell.contentView.layer.add(animation, forKey: "any key")

单元格背景颜色更改动画不起作用。我认为表格单元格滚动动画会覆盖添加的动画。有什么解决方法吗?(注意:我也需要滚动动画)

【问题讨论】:

  • 尝试UIView.animate 而不是CABasicAnimation..
  • @NayanDave . 好的。我会尝试并回复您。
  • @NayanDave,不,它没有帮助
  • 试试DispatchQueue.main.async{ TableView_Scroll_Code }
  • 尝试在 UITableView 的 prepareForReuse 方法中添加动画。该方法用于@Documentation“重置与内容无关的单元格属性,例如alpha、编辑、选择状态。”

标签: uitableview swift5 caanimation


【解决方案1】:

“任何密钥”不是有效密钥。 尝试使用 kCATransition 作为键。

【讨论】:

  • 您好,感谢您的回复。我试过了,但没有奏效。我认为该键与动画没有任何关系。如果我没记错的话,这只是为了唯一标识添加的动画。
猜你喜欢
  • 2017-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
  • 2018-02-24
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多