Phillip Phillip - 1 month ago 12
Swift Question

How to change opacity of animated gif images in iOS

Relevant code below. I want each image to have a fade-in affect while they appear in the overall animation cycle. When running this code, the gif animation affect works, but the opacity changing code does not. Any help would be greatly appreciated. Thanks!

import UIKit

class View1: UIViewController {
@IBOutlet weak var imageView: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var imagesNames = ["ALA0.jpeg", "ALA1.jpeg", "ALA2.png", "ALA3.png", "ALA4.png", "ALA5.png", "ALA6.png", "ALA7.png", "ALA8.png", "ALA9.png", "ALA10.png"]
var images = [UIImage]()

for i in 0..<imagesNames.count{
images.append(UIImage(named: imagesNames[i])!)
imageView.alpha = 0
UIImageView.animate(withDuration: 1.0) {
self.imageView.alpha = 1
}
}

imageView.animationImages = images
imageView.animationDuration = 6.0

self.imageView.startAnimating()
}
}

Joe Joe
Answer

Try below code: Updated

    for i in 0..<imagesNames.count{

        images.append(UIImage(named: imagesNames[i])!)
        imageView.alpha = 0
        UIImageView.animate(withDuration: 0.9, delay: 0, options: [UIViewAnimationOptions.curveEaseInOut,.repeat], animations: { 
             self.imageView.alpha = 1
            }, completion: nil)

    }

       imageView.animationImages = images
       imageView.animationDuration = 5 // I am using 5 images to perform animation

Note: From the above code.You will get a flashy effect frame to frame and you have to play with animation time duration and total animation duration to sync the effect.