不止一次动画细胞

时间:2017-03-21 22:57:55

标签: ios iphone swift swift3

我有一个uitableview单元格,它会在显示单元格时生成动画。这些单元格位于tableviews中,这些tableviews是uitabbarcontroller的选项卡。但是,如果用户第一次按下四个选项卡中的一个,则单元格将设置动画,但如果用户单击新选项卡并返回上一个选项卡,则单元格将不会设置动画。如何在每次呈现tableview时使单元格生成动画。这是我的代码,有人可以告诉我如何在每次呈现视图时使单元格生成动画,而不仅仅是在单元格呈现时。

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

        let rotationTransform = CATransform3DScale(CATransform3DIdentity, 10, 10, 0)
        cell.layer.transform = rotationTransform
        UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.1, options: .curveEaseOut, animations: {
            cell.layer.transform = CATransform3DIdentity
        }, completion: nil)
}

1 个答案:

答案 0 :(得分:3)

在视图上重新加载数据会出现控制器方法:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    tableview.reloadData()
}

此外,您可能需要在再次设置动画之前重置为开始状态。为什么?因为当您滚动表格视图时,出现的单元格将不会生成动画。它们将在动画之后具有最终状态(可重复使用的单元格)。

在完成动画之前,你可能会晃动并且顶部单元格会消失。我不确定会发生什么。也许它不会采取最终状态。当你回到它或那个单元格被重用时,那个单元格将没有最终状态。但是,如果您在重新开始动画之前重置为第一个状态,这将解决所有问题。

重置动画:

cell.layer.transform = nil
// you may need to layout the cell. not sure. after this.
// then the rest of your code that animates the cell.