如何只为 tableViewCell 设置一次动画?

How to animate tableViewCell only once?

我正在处理一个 table 视图,我希望它的单元格在加载时具有动画效果。我在 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
            })
    }

}

这样的事情可能会有所帮助。

创建全局变量

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
            })
        }
    }
}