How To Add Animation To Image Xcode
This guide volition demonstrate one method for creating a repeating "flip book" or animated gif event given multiple images. We volition use a method, animatedImageWithImages()
that can be called on a UIImageView
in gild to cycle through a collection of images at a duration that we will specify.
Pace one: Add together Your Epitome Assets
Make sure the images for your blitheness are all the aforementioned size.
- Add the image avails that you want to use every bit "frames" in your animation to the Avails.xcassets folder. View the Adding Image Avails article to learn how to add epitome avails to your views.
Pace two: Add together an ImageView
We will hold our image animation in a UIImageView. It will need to be the same size as our image "frames".
- Add a UIImageView to your ViewController. You lot can do this in Storyboard or programmatically. View the Using UIImageView commodity to larn how to use the
UIImageView
form. HINT: A elementary mode is to only drag the first image "frame" on to the Storyboard from the Media Library at the bottom of the Utilities pane and it will automatically create an ImageView the correct size of your image frame. - If you created your ImageView in Storyboard, create an outlet to your ViewController code file by ctrl + drag from the ImageView.
Step 3: Declare Your Instance Variables
Instance Variables are declared below the class ViewController: UIViewController {
and above the override func viewDidLoad() {
- Define instance variables for your images.
var loading_1 : UIImage ! var loading_2 : UIImage ! var loading_3 : UIImage !
Nosotros will need to shop our images an array in order to easily reference them for our animation.
- Define an example variable for your
images
array. - Note:
images
is plural because it will contain multiple images.[UIImage]!
ways it will contain a collection of items, each detail beingness of type, UIImage.
var images : [ UIImage ] !
We will shop our blithe paradigm into another variable of blazon UIImage.
- Define an case variable for your animated image.
var animatedImage : UIImage !
Step iv: Assign Values to Your Variables
Assign values to the variables y'all alleged above inside the viewDidLoad()
method.
- Assign the paradigm files in your avails to your image variables.
loading_1 = UIImage ( named : "loading-1" ) loading_2 = UIImage ( named : "loading-2" ) loading_3 = UIImage ( named : "loading-iii" )
- Put your images into your
images
array.
images = [ loading_1 , loading_2 , loading_3 ]
- Call the
animatedImageWithImages
method and store it inanimatedImage
This method takes two arguments- The array of images you want it to cycle through
- The duration you lot want it to cycle at in seconds. Significant information technology will become through all the frames in that time.
animatedImage = UIImage . animatedImage ( with : images , duration : 1.0 )
- Gear up the image inside your ImageView to exist,
animatedImage
.
ImageView . epitome = animatedImage
Footstep 5: Run Your App!
Source: https://guides.codepath.com/ios/Animating-A-Sequence-of-Images
Posted by: hannahofue1976.blogspot.com
0 Response to "How To Add Animation To Image Xcode"
Post a Comment