I want to animate UITableViewCell only once when it displays the first time.
my code:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.alpha = 0
    let transform = CATransform3DTranslate(CATransform3DIdentity, 0, 200, 0)
    cell.layer.transform = transform
    UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
        cell.alpha = 1
        cell.layer.transform = CATransform3DIdentity
    })
}
Animation works great, but the problem is, that animation also gets performed when the user scrolls up direction because of the cell is reused. and that doesn't look good.
I want to show animation once for each cell, how can I achieve that?
You need to record the cell whose animation is done for that you can maintain an array...
Create a property for that.
var arrIndexPath = [IndexPath]()
And then do the following:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if arrIndexPath.contains(indexPath) == false {
        cell.alpha = 0
        let transform = CATransform3DTranslate(CATransform3DIdentity, 0, 200, 0)
        cell.layer.transform = transform
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
            cell.alpha = 1
            cell.layer.transform = CATransform3DIdentity
        })
        arrIndexPth.append(indexPath)
    }
} 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With