我正在处理一个表视图,我希望在加载时将其单元格进行动画。我将alpha淡出从0中淡入1.5秒。我遇到的问题是,每次调用CellForrowat Indexpath都会使这是动画的。是否有一个好方法可以这样做,因此单元格仅一次动画,而不是每次cellforrowat indexpath都被调用?任何帮助都将受到赞赏。谢谢!
// function to configure custom table view cell
func configCell(place: Place) {
// hide image and label before image is loaded
headlineImage.alpha = 0
headlineLabel.alpha = 0
headlineLabel.text = place.name
// set image asynchronously
headlineImage.sd_setImage(with: URL(string: "ImageUrl"), placeholderImage: UIImage(named: "restaurant"), options: .continueInBackground) { (_, _, _, _ ) in
// image download complete fade in image
print("COMPLETED SD SET IMAGE")
UIView.animate(withDuration: 1.5, animations: {
self.headlineImage.alpha = 1
self.headlineLabel.alpha = 1
})
}
}
类似的东西可能会有所帮助。
创建一个全局变量
var isFirstLoad = true
然后在您的configcell方法中,将动画包装在if-block
中,并将标志设置为false
func configCell(place: Place) {
headlineLabel.text = place.name
if isFirstLoad == true {
isFirstLoad = false
headlineImage.alpha = 0
headlineLabel.alpha = 0
headlineImage.sd_setImage(with: URL(string: "ImageUrl"), placeholderImage: UIImage(named: "restaurant"), options: .continueInBackground) { (_, _, _, _ ) in
print("COMPLETED SD SET IMAGE")
UIView.animate(withDuration: 1.5, animations: {
self.headlineImage.alpha = 1
self.headlineLabel.alpha = 1
})
}
}
}