我正在处理一个表格视图,我想要在加载时为其单元格设置动画。我在1.5秒内将alpha淡入淡出从0设置为1。我遇到的问题是每次调用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
})
}
}
答案 0 :(得分: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
})
}
}
}