Shehzad Bilal
Shehzad Bilal

Reputation: 2523

IOS UIButton with animated gif Image

How to add animated gif file as a default image for UIButton. I have added gif file as button image. But it is not animating.

Thanks.

Upvotes: 3

Views: 8663

Answers (4)

jimpic
jimpic

Reputation: 5520

You can not just load a gif file. Save every frame of the gif to a separate image file (png or similar) - you can do that with photoshop for example, then load them into a UIImage using animationImages and do a startAnimating.

Upvotes: 1

sergio
sergio

Reputation: 69047

You should explore alternatives to using UIButton for that, since it does not allow for such customization. E.g., you could use UIImageView` for your button and set an action for the "touch end" event.

Upvotes: 0

Krishnabhadra
Krishnabhadra

Reputation: 34296

Animated GIF won't animate in iphone.. You should add a UIImageView as subview to UIButton, extract all images from your GIF and animate using UIImageView Animation

UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
animatedImageView.animationImages = [NSArray arrayWithObjects:    
                               [UIImage imageNamed:@"image1.gif"],
                               [UIImage imageNamed:@"image2.gif"],
                               [UIImage imageNamed:@"image3.gif"],
                               [UIImage imageNamed:@"image4.gif"], nil];
animatedImageView.animationDuration = 1.0f;
animatedImageView.animationRepeatCount = 0;
[animatedImageView startAnimating];
[yourButton addSubview: animatedImageView];

Upvotes: 16

The iOSDev
The iOSDev

Reputation: 5267

In iOS .gif file format is not supported. so avoid using .gif files in iOS

Upvotes: 0

Related Questions