Code cracker
Code cracker

Reputation: 3166

Stop animating GIF after one revolution

I am using the uiimage-from-animated-gif library to show an animated GIF through a UIImage. It's working, but I need to stop the animation or stop showing imageView2 after one complete revolution.

I animate the GIF with the following code:

NSURL *url2 = [[NSBundle mainBundle] URLForResource:@"dove-animate" withExtension:@"gif"];
self.imageView2.image = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url2]];

Upvotes: 1

Views: 1145

Answers (3)

GPR
GPR

Reputation: 91

Add timer to disappear the imageview. Then it appears like animation stopped

{
NSTimer *timer;

    timer =  [NSTimer scheduledTimerWithTimeInterval: 4
                                              target: self
                                            selector: @selector(removeAnimation)
                                            userInfo: nil
                                             repeats: NO];
}

-(void)removeAnimation
{
imageview.hidden=yes;
}

Upvotes: 0

hlfcoding
hlfcoding

Reputation: 2542

There's no need to resort to gif's if you're storing the images in your bundle and not loading them from the web. You can actually have an entire folder of images (png's would be good quality and offer transparency). Then:

  1. Load in each image into a UIImage via UIImage + imageNamed: or similar.
  2. Create the UIImageView and set animationImages to your array.
  3. Call startAnimating and stopAnimating on the image view as desired.

Upvotes: 1

BHASKAR
BHASKAR

Reputation: 1201

If you know the animation duration then you can just set a static image to your image view after that duration.

For example, if the duration is 2 seconds, then

[self performSelector:@selector(stopAnimating) withObject:nil afterDelay:2.0f];

-(void)stopAnimating{
    [self.imageView2 setImage:[UIImage imageNamed:@"dove-animate.gif"]];
}

Upvotes: 1

Related Questions