我有一个UITableView可以加载包含大量图像和其他动画的单元格,它可以正常工作,但是它看起来很慢,因为在向上或向下滚动时将其重命名为cellForRowAt它会重新加载图像和动画,是否可以避免这种情况这些单元格之前已经加载过?
答案 0 :(得分:0)
只需将动画逻辑移至willDisplayCell委托,并且仅在我第一次使用标志进行动画处理时即可。
代码示例:
let popularLinks = ["One","Two","Three","Four","Five"]
var isAnimated = false
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
var lastInitialDisplayableCell = false
if popularLinks.count > 0 && !isAnimated {
if let indexPathsForVisibleRows = tableView.indexPathsForVisibleRows,
let lastIndexPath = indexPathsForVisibleRows.last, lastIndexPath.row == indexPath.row {
lastInitialDisplayableCell = true
}
}
if !isAnimated {
if lastInitialDisplayableCell {
isAnimated = true
}
cell.transform = CGAffineTransform(translationX: 0, y: cell.bounds.height/2)
cell.alpha = 0
UIView.animate(withDuration: 0.5, delay: 0.05*Double(indexPath.row), options: [.curveEaseInOut], animations: {
cell.transform = CGAffineTransform(translationX: 0, y: 0)
cell.alpha = 1
}, completion: nil)
}
}