5588

How to change opacity of animated gif images in iOS

Question:

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() } }

Answer1:

Try below code: <strong>Updated</strong>

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

<strong>Note:</strong> 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.

Answer2:

The problem is your animation is inside your for loop.

You have 11 images, so the 4 lines that execute the animation, from imageView.alpha = 0, are being executed 11 times, that's going to confuse the animation system.

You should set the alpha to 0 in the same place you set the animation images or the duration instead.

As for the actual animation, you should put that in viewDidAppear:. You don't want your animation to start until the view is in the screen, otherwise it might seem like it doesn't start from the beginning.

Finally, a nicer way of instantiating the images array would be

let images = imageNames.flatMap { UIImage(named: $0) }

Even Swiftier, no need to declare all the names (Also, UIImage(named:) will find the image extension for you, no need to add it.)

let images = (0..<11).flatMap { UIImage(named: "ALA\($0)") }

Recommend

  • Google Apps Marketplace SDK install does not forward user to Setup URL
  • Use double quote then curly brace in powershell -Command
  • this class is not key value coding-compliant for the key PerformRotate
  • Xcode 7 - How to initiate interface recording for UI Testing?
  • How to access data/data files of Android without a terminal on the Android device
  • Grunt module(s) to upload all static assets to S3 / CloudFront, replace paths, and invalidate old as
  • Access PCF DEV from external machine on same network as host
  • LCD Programming with Arduino
  • Why does the first run of “XCTestCase -measureBlock:” takes so much time?
  • how to avoid the dependencies hell with unit test in angular 2+
  • ORA-12154: TNS:could not resolve the connect identifier specified
  • Getting EOFError along with exceptions when using ftplib
  • including Python.h in C++ file CDT
  • Error Installing Windows 10 development tools for Visual Studio 2015 RC on Windows 8.1
  • Is it possible to make imports depend on the location of my Lua script instead of the current direct
  • How to change default stop edit behavior in jtable
  • How to use SBT with multiple sub project web applications?
  • Arduino making decision according to a packet received from serial port
  • Can't compile Arduino CapSense example
  • Matplotlib save file as JPEG encoder error
  • Read the values from XML file
  • MVVM: Image Bind Source from FileOpenPicker
  • matlab crashes without dump file when using fopen for file
  • Prevent page break in text block with iText, XMLWorker
  • Python 3.2.2, error(scripts to exe)
  • NSIS decompiler
  • Floated image with variable width and heading with background image
  • Best way to dynamically load an xml configuration file into a Flex4/Flash movie at runtime?
  • Can't delete or rename original file after resizing
  • Problem while Building a Setup Project for a windows Service?
  • How to have background script and something similar to a default popup?
  • JQuery Internet Explorer and ajaxstop
  • Python urlparse: small issue
  • How to delay loading a property with linq to sql external mapping?
  • Cannot connect to cassandra from Spark
  • GridView Sorting works once only
  • Cannot Parse HTML Data Using Android / JSOUP
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Getting Messege Twice Using IMvxMessenger
  • How to load view controller without button in storyboard?