I have an array of images which I want to animate by playing these images one after the other in a sequence. I want to repeat the whole loop several times. I am developing a game for iPad. Suggest to me a method to achieve this functionality in Objective-C with the Cocoa framework.
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Xcode: Is there a way to change line spacing (UI L
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
add your own images in the array.repeat Count 0 means infinite loop.You can give your own number also.
See the
animationImages
property ofUIImageView
. It’s hard to say if it fits your needs as you don’t give us details, but it’s a good start.There are at least 3 ways to animate an array of images through a
UIImageView
. I'm adding 3 links to download sample code for the 3 possibilities.The first one is the one that everyone knows. The other ones are less known.
- UIImageView.animationImages
Example Link
The problem of this one is that do not have Delegate to tell us in which moment the animation is finished. So, we can have problems if we want to display something after the animation.
In the same way, there is no possibility to kept the last image from the animation in the
UIImageView
automatically. If we combine both problems we can have a gap at the end of the animation if we want to kept the last frame on screen.- CAKeyframeAnimation
Example Link
This way to animate works through
CAAnimation
. It have an easy delegate to use and we can know when the animation finish.This is probably the best way to animate an array of images.
Delegate:
- CADisplayLink
Example Link
A CADisplayLink object is a timer object that allows your application to synchronize its drawing to the refresh rate of the display.
This way to do it is really interesting and opens a lot of possibilities to manipulate what are we showing in screen.
DisplayLink getter:
Methods:
GENERAL PROBLEM:
Even though we have this 3 possibilities, if your animation is with a lot of big images, consider using a video instead. The usage of memory will decrease a lot.
A General problem you will face doing this is in the moment of the allocation of the images.
If you use [UIImage imageNamed:@"imageName"] you will have cahe problems.
From Apple:
This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method locates and loads the image data from disk or asset catelog, and then returns the resulting object. You can not assume that this method is thread safe.
So,
imageNamed:
stores the image in a private Cache.- The first problem is that you can not take control of the cache size.
- The second problem is that the cache did not get cleaned in time and if you are allocating a lot of images with
imageNamed:
, your app, probably, will crash.SOLUTION:
Allocate images directly from
Bundle
:Small problem:
Images in
Images.xcassets
get never allocated. So, move your images outsideImages.xcassets
to allocate directly from Bundle.I have added a swift 3.0 extension for this
This is a simple and working code for animation./
Best solution for me use CADisplayLink. UIImageView doesn't have completion block and you can't catch steps of animation. In my task i must changing background of view with image sequencer step by step. So CADisplayLink allows you handling steps and finishing animation. If we talk about usage of memory, i think best solution load images from bundle and delete array after finishing
ImageSequencer.h
ImageSequencer.m