Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImage animatedImageNamed repeat count

I have a images sequence animated with :

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 120)];
[UIImage animatedImageNamed:@"spinner-" duration:1.0f];
imgView.contentMode = UIViewContentModeCenter;
imgView.animationRepeatCount = 1; // No effect
[self.view addSubView:imgView];

But the sequence keeps looping. Is there a way to control the repeat count (and keep displaying the last image) ?

like image 652
kursus Avatar asked Mar 06 '15 13:03

kursus


1 Answers

In your case, using [UIImage animatedImageNamed:@"spinner-" duration:1.0f]; won't give you what you want.

For what you would like to achieve, I would recommend using UIImageView.

Usage:

First create an UIImageView object:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:<FRAME_SIZE>];

Second, create an array that holds your images to be animated.

If you just have a few, do the following:

NSMutableArray *animatedImagesArray = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:@"spinner-1.png"], [UIImage imageNamed:@"spinner-2.png"], nil];

If you have more, then you could load your images into the array like this:

for (int i = 0; i < <NUM_OF_IMAGE_COUNT>; i++)
{
    [animatedImagesArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"spinner-%d%@", i, @".png"]]];
}

Once all that is done, then we configure the controls you want:

imageView.animationImages = animatedImagesArray;

imageView.animationDuration = 1.0f;

imageView.animationRepeatCount = 1.0f;

Then start animating it by:

[imageView startAnimating];

And stop the animation via:

[imageView stopAnimating];

If you would like the UIImageView to hold the last image after stopping, we would need to carry out the following before starting the animation:

imageView.image = [imageView.animationImages lastObject];
....

[imageView startAnimating];

Hope this helps.

like image 69
Unheilig Avatar answered Oct 13 '22 07:10

Unheilig