标签:
------------- ViewController.m -------------
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)drink;
- (IBAction)konckOut;
- (IBAction)footLeft;
@end
@implementation ViewController
- (IBAction)drink
{
[self playAnimationWithImageName:@"drink" andImageCount:81];
}
- (IBAction)konckOut
{
[self playAnimationWithImageName:@"knockout" andImageCount:81];
}
- (IBAction)footLeft
{
[self playAnimationWithImageName:@"footLeft" andImageCount:30];
}
- (void) playAnimationWithImageName:(NSString *) imageName andImageCount:(NSInteger) count
{
if(self.imageView.isAnimating)
{
return;
}
NSMutableArray *images = [NSMutableArray array];
for (int i = 0; i < count; i++)
{
NSString *name = [NSString stringWithFormat:@"%@_%02d.jpg",imageName,i];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:path];
[images addObject:image];
}
self.imageView.animationImages = images;
self.imageView.animationDuration = 0.05 * count;
self.imageView.animationRepeatCount = 1;
[self.imageView startAnimating];
// 当动画播放完毕需要清除animationImages
// 第一种方式:
[self performSelector:@selector(clearImages) withObject:nil afterDelay:self.imageView.animationDuration];
// 第二种方式
// [self.imageView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.imageView.animationDuration];
// 第三种方式
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.imageView.animationDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// self.imageView.animationImages = nil;
// });
}
- (void) clearImages
{
self.imageView.animationImages = nil;
}
@end
标签:
原文地址:http://www.cnblogs.com/lixiang2015/p/4697126.html