2

I have a custom UICollectionViewCell within a UICollectionView that animates correctly when the collection view is initially displayed. However when I segue into a new UIViewController and back to the original one, the animation stop.

class EmptyDishesCollectionViewCell: UICollectionViewCell {

  @IBOutlet weak var plateImage: UIImageView!
  @IBOutlet weak var knifeImage: UIImageView!
  @IBOutlet weak var forkImage: UIImageView!

  @IBOutlet weak var emptyBackgroundView: UIView!
  @IBOutlet weak var plateHeight: NSLayoutConstraint!
  @IBOutlet weak var plateWidth: NSLayoutConstraint!

  override func awakeFromNib() {
    super.awakeFromNib()
    setupBorders()
    setupImages()
    beginAnimation()
    setShadow()
  }

  func setupBorders() {
    self.emptyBackgroundView.layer.cornerRadius = 3.0
    self.emptyBackgroundView.backgroundColor = UIColor.white
  }

  func setupImages() {
    self.plateImage.tintColor = UIColor.projectBackgroundSuperLightGray
    self.forkImage.tintColor = UIColor.projectBackgroundSuperLightGray
    self.knifeImage.tintColor = UIColor.projectBackgroundSuperLightGray
  }

  func beginAnimation() {
    UIView.animateKeyframes(withDuration: 2.4, delay: 0.0, options: [.calculationModeLinear, .repeat, .autoreverse], animations: {
      UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 2/5, animations: {
        let moveLeft = CGAffineTransform(translationX: -5.0, y: 0.0)
        let moveRight = CGAffineTransform(translationX: 5.0, y: 0.0)
        let rotate = CGAffineTransform(rotationAngle: .pi / 5.0)
        self.forkImage.transform = moveLeft
        self.knifeImage.transform = moveRight
        self.plateImage.transform = rotate
      })
      UIView.addKeyframe(withRelativeStartTime: 1/3, relativeDuration: 1/5, animations: {
//        let wait = CGAffineTransform(translationX: -5.0, y: 0.0)
//        self.forkImage.transform = wait
      })
      UIView.addKeyframe(withRelativeStartTime: 2/3, relativeDuration: 2/5, animations: {
        self.forkImage.transform = .identity
        self.knifeImage.transform = .identity
        self.plateImage.transform = .identity
      })
    })
  }
}

I'm assuming when a view is transitioned out of sight, the animation automatically stops, however when it's put back into view it needs to be kicked off again manually.

so naturally I thought putting the animation block into didMoveToWindow would fix it, but it was a no go.

Any help would be appreciated

enter image description here.

Reza Shirazian
  • 2,303
  • 1
  • 22
  • 30

1 Answers1

2

How about collectionView.reloadData() in your viewWillAppear(animated)? I'm guessing that won't help unless you call your beginAnimation() in your cellForItemAt.

So you're calling beginAnimation() inside your awakeFromNib(),

From: https://stackoverflow.com/a/9122393/3231194

awakeFromNib gets called after the view and its subviews were allocated and initialized. It is guaranteed that the view will have all its outlet instance variables set.

I'm proposing you just call your EmptyDishesCollectionViewCell's beginAnimation() function during cellForItemAt. And if necessary, just reload your collectionView. Hope it helps!.

Glenn Posadas
  • 12,555
  • 6
  • 54
  • 95
  • Calling `collectionView.reloadData()` in `viewWillAppear(animated)` triggered the `collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)` and adding `cell.beginAnimation()` in the `cellForItemAt` fixed it. It seems a bit heavy handed to reloaded the whole collection but it works. Thank you – Reza Shirazian Oct 13 '17 at 17:03