Skip to content

Instantly share code, notes, and snippets.

@braking
Created May 3, 2013 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save braking/5508906 to your computer and use it in GitHub Desktop.
Save braking/5508906 to your computer and use it in GitHub Desktop.
iOS Animated Gif implementation code. This is the contents of a ViewController.m that has an image view that should toggle a set of images back and forth like a gif.
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, weak) IBOutlet UIImageView *starImageView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *starImageArray = [@[] mutableCopy];
for (int i = 0; i < 8; i++) {
UIImage *starImage = [UIImage imageNamed:[NSString stringWithFormat:@"star_%d", i]];
[starImageArray addObject:starImage];
}
self.starImageView.animationImages = starImageArray;
self.starImageView.animationRepeatCount = 1;
self.starImageView.animationDuration = 0.267;
self.starImageView.image = starImageArray[0];
}
- (IBAction)animateButtonTouched
{
if (!self.starImageView.isAnimating) {
[self.starImageView startAnimating];
[self.starImageView setImage:[self.starImageView.animationImages lastObject]];
[self performSelector:@selector(starAnimationDidFinish) withObject:nil afterDelay:self.starImageView.animationDuration];
}
}
- (void)starAnimationDidFinish
{
NSArray *reversedImages = [[self.starImageView.animationImages reverseObjectEnumerator] allObjects];
self.starImageView.animationImages = reversedImages;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment