Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableview Cell animation play only once

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?

like image 728
Uday Babariya Avatar asked Dec 19 '22 00:12

Uday Babariya


1 Answers

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)
    }
} 
like image 58
Mahendra Avatar answered Jan 05 '23 16:01

Mahendra