UIImageView startAnimating: How do you get it to stop on the last image in the series?
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;
[m开发者_JS百科yAnimation startAnimating];
Could I have to use override the startAnimating method? What is the best way to fix my problem?
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.
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 :(
精彩评论