3

When I display my tableview I've made my cells come with an animation like this.

 func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

             cell.alpha = 0
             let rotation = CATransform3DTranslate(CATransform3DIdentity, -250, 20, 0)
             cell.layer.transform = rotation
             UIView.animateWithDuration(0.9){
             cell.alpha = 1
             cell.layer.transform = CATransform3DIdentity

    }

It's working like this but when i go down and then up again to see the previous cells the animation still exists.

Show I believe I have to check if the cell has been displayed.

Something I try is to use didEndDisplayingCell function and put the cell.tag inside an array and then I was checking if the current cell is inside that array with array.contains(cell.tag) but it didn't work. Actually the animation worked only for the three first cells and then nothing.

mike vorisis
  • 2,786
  • 6
  • 40
  • 74

3 Answers3

5

You should keep an array of indexPaths, adding to it whenever a cell is displayed. This way you can check if the cell has already animated.

if (!indexPaths.contains(indexPath)) {
    indexPaths.append(indexPath)
    cell.alpha = 0
    let rotation = CATransform3DTranslate(CATransform3DIdentity, -250, 20, 0)
    cell.layer.transform = rotation
    UIView.animateWithDuration(0.9){
        cell.alpha = 1
        cell.layer.transform = CATransform3DIdentity
    }
}
James P
  • 4,786
  • 2
  • 35
  • 52
1

As @James-P mentioned in comments - just create an array

var indexPathArray = [NSIndexPath]()

and rewrite your willDisplayCell method:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    if !indexPathArray.contains(indexPath) {

        indexPathArray.append(indexPath)

        cell.alpha = 0
        let rotation = CATransform3DTranslate(CATransform3DIdentity, -250, 20, 0)
        cell.layer.transform = rotation
        UIView.animateWithDuration(0.9){
            cell.alpha = 1
            cell.layer.transform = CATransform3DIdentity
        }
    }
}
slxl
  • 635
  • 1
  • 10
  • 23
1

what you can do is put the indexpath of cell in an array and check if the indexpath is in array.if the indexpath is in array dont perform animation

harpreetSingh
  • 1,354
  • 1
  • 10
  • 12