I have a UImageview with animated image. i am adding the uiimageview in code and its a part of a CollectionViewCell When the user touches the cell the animation stops, why does this happen?
code:
var images: [UIImage] = []
for i in 0...10 {
images.append(UIImage(named: "image\(i)"))
}
let i = UIImageView(frame: CGRect(x: xPos, y: yPos, width: 200, height: 200))
i.animationImages = images
i.animationDuration = 0.5
i.startAnimating()
i.contentMode = UIViewContentMode.Center
i.userInteractionEnabled = false
self.addSubview(i)
If you don't want any interaction then following will be the fastest way to resolve this issue:
collectionView.allowsSelection = false
In your custom collection view cell class, write following methods to fix issue
func setSelected(selected:Bool) {
}
func setHighlighted(higlighted:Bool) {
}
Swift 4.0 Version:
override open var isSelected: Bool
{
set {
}
get {
return super.isSelected
}
}
override open var isHighlighted: Bool
{
set {
}
get {
return super.isHighlighted
}
}
Overriding isSelected, isHighlighted with empty setter will solve this issue, but it will lose those two properties to be set. I was able to solve this issue by calling imageView.startAnimating() at didSelectItemAt in UICollectionViewDelegate.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let item = items[indexPath.item]
if item.hasGIF {
let cell = collectionView.cellForItem(at: indexPath) as! ItemCell
cell.imageView.startAnimating()
}
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let item = items[indexPath.item]
if item.hasGIF {
let cell = collectionView.cellForItem(at: indexPath) as! ItemCell
cell.imageView.startAnimating()
}
}
In my case, I can't disable selection or use any of the solutions posted before here. I do not need to highlight the cell so I disabled it through the delegate method below to return false which prevented the stopAnimating() method from being called. This was an issue I encountered when using the AnimatedImageView of KingFisher used in a UICollectionViewCell.
func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
return false
}
An array of UIImage objects to use for an animation. var highlightedAnimationImages: [UIImage]? An array of UIImage objects to use for an animation when the view is highlighted. The amount of time it takes to go through one cycle of the images.
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