I am making a game that has a win state and a lose state. For each state, I would like to display a short animation of 2-3 seconds. The first animation consists of 60 PNGs, at a rate of 30 fps, and the second animation consists of 80 PNGs, also at 30fps. I have tried various methods, and searched extensively for a solution, but the top two results I keep finding are to use UIImageView animationImages and Core Animation. Using UIImageView works very nicely in the simulator, but takes forever to run the first time on the device. I figure this is because the images need to be loaded into memory. I am using “imageNamed” to populate the animationImages array, but I guess they need to be rendered the first time the animation is called? I have tried pre-rendering them when the app loads, and this seems to do the trick, but pre-rendering all those images takes nearly 10 seconds, which is not user friendly. I would also be open to using Core Animation, but I have very limited knowledge of it so a detailed explanation would be greatly appreciated. Thanks!
when you say that I need to load everything at the app’s startup, what exactly do you mean? Right now, I initialize both arrays of animation images when the app loads, like this:
// Create thinnestAnimation Array
thinnestAnimation = [[NSMutableArray alloc] init];
for(int i = 1; i <= 60; i++)
{
UIImage *thinImage = [UIImage imageNamed:[NSString stringWithFormat:@"ThinnestJump_%d.png", i]];
[thinnestAnimation addObject:thinImage];
}
thinAnimationImageView.animationImages = (NSArray *)thinnestAnimation;
thinAnimationImageView.animationDuration = 2.0;
thinAnimationImageView.animationRepeatCount = 1;
thinAnimationImageView is an IBOutlet in my interface, and I call [thinAnimationImageView startAnimating]; when I want to animate the sequence of images. This heavily lags on the first call, but is fine thereafter. Is there anyway to prevent it from lagging the very first time?
UIImageView‘s built-in animation features are a good way of doing it. However, you need to load everything before you need to animate, such as at the app’s startup. If you need multiple different animations to be ready, load multiple UIImageView objects at startup and call the one you need later, with itsstartAnimatingmethod.There are other methods to animate as well, such as using Cocos2D which uses OpenGL for animation, much faster performance. But then you have build your app in a different framework altogether, which might be overkill depending on what else is going on in your app.