UIImageView animated doesn't work properly inside UICollectionViewCell UIImageView animated doesn't work properly inside UICollectionViewCell swift swift

UIImageView animated doesn't work properly inside UICollectionViewCell


When you select the cell, the image view looks to its highlightedAnimationImages, not its animationImages. You didn't set the highlightedAnimationImages so you see nothing.

Here's a screencast showing that this can work. The cell is selected when the background is gray (that is its selectedBackgroundView) but the animation continues:

enter image description here


In case anyone else encounters this nasty bug and the above solutions don't help – try calling imageView.stopAnimating() in your cell's prepareForReuse() function.


Swift 4 version of Solution 2 from @Sebastian.

func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {    if let cell = collectionView.cellForItem(at: indexPath) as? CustomCell {        cell.imgView.startAnimating()    }}func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {    if let cell = collectionView.cellForItem(at: indexPath) as? CustomCell {        cell.imgView.startAnimating()    }}