Created
May 3, 2013 12:46
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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