3

I am using UIImageView to create a simple animation and everything works perfectly, but when I use startAnimating, the animation will run once (as I want it to) but go back to the first frame. I want the animation to stop on the last frame.

I use:

myAnimation.animationImages = myArray;
myAnimation.animationDuration = 0.7;
myAnimation.animationRepeatCount = 1;
[myAnimation startAnimating];

Could I have to use override the startAnimating method? What is the best way to fix my problem?

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
Derek
  • 311
  • 1
  • 4
  • 5

2 Answers2

8

Before you start animating, set the last image in the image view imageView.image = yourLastImage; and then start the animation. After the animation is complete the image view will show yourLastImage.

As for the NSTimer thing, theres no other alternative to know when the default animation has ended. So if you want to do anything after the animation completes, you have to rely on NSTimer as deanWombourne suggested.

Swapnil Luktuke
  • 10,385
  • 2
  • 35
  • 58
  • Thanks. How exactly would I retrieve the last image from an imageView with animationImages set? (In the API it says under UIImageView image: "If the animationImages" property contains a value other than nil, the contents of this property are not used.) – Derek Jul 28 '10 at 12:12
  • Oh I got it. For anyone who is wondering, it's: imageView.image = [imageView.animationImages objectAtIndex:int]; – Derek Jul 28 '10 at 12:18
0

It's very annoying - I'd like to see a deleate property so we can find out animation events!

Here's how I have done it in the past (there may be a better way!)

1) Set the animation images property and start the animation (as in your code in the question)

2) Schedule an NSTimer to go off in 'animationDuration' seconds time

3) When the timer goes off, set the image property to be the last object in the animationImages array and then set the animationImages property to nil.

EDIT: Ron Srebro has correctly pointed out a possilbe bug - if the timer is delayed at all then the animation will look wrong.

I fixed this by adding the last frame to the animationImages twice - this means that if the timer is delayed, we are still showing the last frame and the user will never notice.

NB This won't 100% fix this bug but I've never seen it happen with this fix in place. It might not work for apps that are CPU intensive as the timer might be delayed by more than one frame duration :(

Community
  • 1
  • 1
deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • I agree it's very annoying, but the chances of your suggestion actually working is very slim. I don't think you can count on NSTimer to fire exactly at the same moment the image view displays the last frame. – Ron Srebro Jul 28 '10 at 11:31
  • Ooops, I'd forgotten that - what I tend to do is to add the last frame again to the animations array - that way if the timer is a little late and it's still animating, it doesn't look like it is ;) I'll edit my answer! – deanWombourne Jul 28 '10 at 11:50