【问题标题】:Animating cell more than once不止一次动画单元格
【发布时间】:2017-03-21 22:57:55
【问题描述】:

我有一个 uitableview 单元格,它会在显示单元格时生成动画。这些单元格在表格视图中,这些表格视图是 uitabbar 控制器的选项卡。但是,如果用户第一次按下四个选项卡中的一个,则单元格将设置动画,但如果用户单击新选项卡并返回到前一个选项卡,则单元格将不会设置动画。我怎样才能使单元格在每次呈现 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)
}

【问题讨论】:

    标签: ios iphone swift swift3


    【解决方案1】:

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

    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.
    

    【讨论】:

    • 我把这个 cell.layer.transform = nil 放在哪里
    • 在您制作动画之前将显示。它会在动画之前重置为起始状态,以防止不需要的行为
    • 并允许尚未出现的底部单元格动画。
    • 所以它将是 willDisplay 中的第一行,就在此之前 let rotationTransform = CATr
    • 我不确定 cell.layer.transform = nil 是否足以重置动画。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多