我一直在试图弄清楚如何在不需要外部数组的情况下将动画应用于表视图的单元格。我希望动画仅在用户向下滚动时发生一次,但在用户向下滑动以重新加载数据时重置。我选择了布尔变量 animated已经在自定义单元格类定义中 - 它可以工作,但动画注册得如此之快,并且动画的显示不一致。另外,我正在努力寻找一种方法来重置刷新表格视图时每个单元格中 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)
}
表格单元格中同步动画非常困难。
秘诀是你需要一个信号系统 - 它独立运行,独立于应用程序中的其他所有内容 - 并跟踪时间。
该信号系统单例 - 每个单元都应连接到它并基于此进行动画处理。 (或者,根据您的风格,信号系统连接到细胞并告诉它们该怎么做。