Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImageView animationRepeatCount strange behaviour

So i have this code that animates a set of images an unlimited amount of times just fine, however as soon as i try to limit the number of times it animates it simply does not work. No image is even displayed.

Code that works:

animatedMap.animationImages = [NSArray arrayWithObjects:
                               [UIImage imageNamed:@"0.gif"],
                               [UIImage imageNamed:@"1.gif"],
                               [UIImage imageNamed:@"2.gif"],
                               [UIImage imageNamed:@"3.gif"],
                               //etc...];
animatedMap.animationDuration = 20.0f;
animatedMap.animationRepeatCount = 0;
[animatedMap startAnimating];

Code that doesn't work:

animatedMap.animationImages = [NSArray arrayWithObjects:
                               [UIImage imageNamed:@"0.gif"],
                               [UIImage imageNamed:@"1.gif"],
                               [UIImage imageNamed:@"2.gif"],
                               [UIImage imageNamed:@"3.gif"],
                               //etc...];
animatedMap.animationDuration = 20.0f;
animatedMap.animationRepeatCount = 1;
[animatedMap startAnimating];

This just seems like really strange behaviour?

like image 710
Joe Maher Avatar asked Mar 17 '23 10:03

Joe Maher


2 Answers

I noticed the same behavior, my solution was to put the start of animation code in a different place.

For me, it was a View Controller, so I put imageView.startAnimating() in viewWillAppear: instead of viewDidLoad:.

I don't 100% understand what is the problem, but one can assume that the animation doesn't fire because by setting the animationRepeatCount to 1, your "one animation" happens before the view is displayed. To be more technical, the CATransaction::Commit with the one animation gets performed before the view is displayed, and not when the view is displayed.

like image 169
kgaidis Avatar answered Mar 31 '23 13:03

kgaidis


animationRepeatCount is used for cycle all the images in given animationDuration duration.

So if you provide animatedMap.animationRepeatCount = 1; that means only 1 cycle will be run and then the default set image will be displayed. If you didn't define default image to UIImageView then it will be blank(i.e. background color of UIImageView )

There is possibility to have more images then the given time duration for animation, which leads to super-fast animation.

Update 1

You can set last image just before starting your animation.

NSArray *imgArray = [NSArray arrayWithObjects:
                           [UIImage imageNamed:@"0.gif"],
                           [UIImage imageNamed:@"1.gif"],
                           [UIImage imageNamed:@"2.gif"],
                           [UIImage imageNamed:@"3.gif"],
                           //etc...];
[animatedMap setImage:[UIImage imageNamed:[imgArray lastObject]]];
animatedMap.animationImages = imgArray
animatedMap.animationDuration = 20.0f;
animatedMap.animationRepeatCount = 0;
[animatedMap startAnimating];
like image 32
Viral Savaj Avatar answered Mar 31 '23 12:03

Viral Savaj