首页 > 解决方案 > 有没有办法抵消 tableview 动画,使它们不会一次全部显示?

问题描述

我一直在试图弄清楚如何在不需要外部数组的情况下将动画应用于 tableview 的单元格。我希望动画在用户向下滚动时只发生一次,但在用户向下滑动以重新加载数据时重置。我在自定义单元格类定义中选择了一个布尔变量 animatedAlready ——它可以工作,但是动画注册得太快了,而且动画出现的不一致。另外,我正在努力寻找一种方法来在刷新 tableview 时重置每个单元格中 animatedAlready 的值...

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    //If transactionPage is set to -1, it's because we've reached the end of the transactions
    if transactionPage != -1 && indexPath.row == (tableData.rows(inSection: indexPath.section).count) - 1 {
        loadMoreTransactions()
    }

    if let cell = cell as? CustomCell, !cell.animatedAlready {
        cell.alpha = 0
        cell.animatedAlready = true

        //Slide from bottom
        let transform = CATransform3DTranslate(CATransform3DIdentity, 0, 200, 0)

        cell.layer.transform = transform

        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
            cell.alpha = 1
            cell.layer.transform = CATransform3DIdentity
        })
    }
}

以及我当前重新加载动画的尝试:

    @objc func reloadBalanceAndTransactions() {
        refreshControl.beginRefreshing()

        tableView.reloadWithAnimation()

        //Reset
        transactionPage = 0
        callbackCount = 2

        loadBalance()
        loadMoreTransactions()
    }

扩展名(我完全知道这是错误的,只是想不出别的):

func reloadWithAnimation() {
        self.reloadData()
        let tableViewHeight = self.bounds.size.height
        let cells = self.visibleCells
        var delayCounter = 0
        for cell in cells {
            cell.transform = CGAffineTransform(translationX: 0, y: tableViewHeight)
        }
        for cell in cells {
            UIView.animate(withDuration: 1.6, delay: 0.08 * Double(delayCounter),usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {
                cell.transform = CGAffineTransform.identity
            }, completion: nil)
            delayCounter += 1
        }
    }

    func scrollToTop(animated: Bool = false) {
        self.scrollToRow(at: IndexPath(row: 0), at: .top, animated: animated)
    }

标签: iosswift

解决方案


很难做到这一点。在表格单元格中同步动画。

秘诀是你需要一个信号系统——它独立运行,独立于应用程序中的其他一切——并且可以跟踪时间。

该信号系统单例 - 每个单元都应连接到它并基于它进行动画处理。(或者,根据您的风格,信号系统会连接到细胞并告诉他们该做什么。)

享受!


推荐阅读