4

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?

Joe Maher
  • 4,797
  • 4
  • 22
  • 39

2 Answers2

5

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.

kgaidis
  • 10,753
  • 4
  • 58
  • 76
  • YES!! Thank you, this was my problem too. Setting everything up and calling startAnimating() in viewDidLoad() worked fine, until I tried to set animationRepeatCount to non-zero, then the image just disappeared. Moving the setup to viewWillAppear made it work perfectly! – Keith Jan 29 '16 at 15:26
  • I have the same issue but it is happening when I try to animate from a speech synthesizer delegate did finish method. https://stackoverflow.com/questions/60766734/animation-not-playing-inside-speech-synthesizer-delegate?noredirect=1#comment107525325_60766734 – JRowan Mar 20 '20 at 16:52
2

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];
Viral Savaj
  • 3,311
  • 1
  • 23
  • 38