Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uiimageview animation stops when user touches screen

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) 
like image 725
ilan Avatar asked Jan 12 '15 14:01

ilan


5 Answers

If you don't want any interaction then following will be the fastest way to resolve this issue: collectionView.allowsSelection = false

like image 181
Ahmed Avatar answered Nov 18 '22 05:11

Ahmed


In your custom collection view cell class, write following methods to fix issue

func setSelected(selected:Bool) {

}

func setHighlighted(higlighted:Bool) {

}
like image 36
Mohith Km Avatar answered Nov 18 '22 05:11

Mohith Km


Swift 4.0 Version:

override open var isSelected: Bool
{
    set {

    }

    get {
        return super.isSelected
    }
}

override open var isHighlighted: Bool
{
    set {

    }

    get {
        return super.isHighlighted
    }
}
like image 26
seb Avatar answered Nov 18 '22 06:11

seb


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()
    }
}
like image 24
Sukwon Avatar answered Nov 18 '22 04:11

Sukwon


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.

like image 1
naz Avatar answered Nov 18 '22 04:11

naz