Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I stop UIImageView Animation at last frame?

I have an animation using a UIImageView

myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = 1;
myAnimatedView.animationRepeatCount = 1;
[myAnimatedView startAnimating];

How can I tell to animation to stop at the last frame or to be visible last frame of the series of images?

Thank you in advance

like image 464
Ivan Avatar asked May 05 '10 20:05

Ivan


4 Answers

the image property on the UIImageView class has the following docs: "If the animationImages property contains a value other than nil, the contents of this property are not used."

So the trick to hold on the last frame of an animation in iOS4 is to first set the image property to that last frame (while animationImages is still nil), then set the animationImages property and call startAnimating. When the animation completes, the image property is then displayed. No callback/delegate needed.

like image 87
Brian Bahner Avatar answered Oct 22 '22 01:10

Brian Bahner


You can set image of last frame to your UIImageView instance and then when animation will finish last frame image remains visible. Here is the code:

// create your array of images
NSArray *images = @[[UIImage imageNamed:@"video1.png"], [UIImage imageNamed:@"video2.png"]];
UIImageView *imageView = [UIImageView new];
imageView.image = images.lastObject;
imageView.animationImages = images;
imageView.animationDuration = 1.0; // seconds
imageView.animationRepeatCount = 1;
[imageView startAnimating];
like image 26
Bogdan Avatar answered Oct 22 '22 03:10

Bogdan


After the animation finishes you can set your UIImageView image to the last frame of your animation.

myAnimatedView.image = [myAnimatedImages objectAtIndex:myAnimatedImages.count - 1]

I don't think there is a delegate method that would notify you on animationFinish event so you would need to start a NSTimer with same duration as the animation to get the notification.

UIImageViewAnimation documentation

like image 6
texmex5 Avatar answered Oct 22 '22 02:10

texmex5


Swift version will be as given below but in objective-c logic will be same
you can hack it just setting the image of the UIImageView to the last image of the images array. just before the code of animation, like imageView.image = arrayOfImage.last
the whole logic is given below

        imageView.image = arrayOfImage.last //(this line create magic)
        imageView.animationImages = arrayOfImage
        imageView.animationDuration = 1.0
        imageView.animationRepeatCount = 1
        imageView.startAnimating()
like image 3
Ali Subhani Avatar answered Oct 22 '22 01:10

Ali Subhani