/** 这是一只显示图片的猫 */
@property (weak, nonatomic) IBOutlet UIImageView *tom;
@end
@implementation MJViewController
/** 播放动画 */
- (void)runAnimationWithCount:(int)count name:(NSString *)name
{
if (self.tom.isAnimating) return;
// 1.加载所有的动画图片
NSMutableArray *images = [NSMutableArray array];
for (int i = 0; i<count; i++) {
// 计算文件名
NSString *filename = [NSString stringWithFormat:@"%@_%02d.jpg", name, i];
// 加载图片
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:filename ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:path];
// 添加图片到数组中
[images addObject:image];
}
self.tom.animationImages = images;
// 2.设置播放次数(1次)
self.tom.animationRepeatCount = 1;
// 3.设置播放时间
self.tom.animationDuration = images.count * 0.05;
[self.tom startAnimating];
// 4.动画放完1秒后清除内存
CGFloat delay = self.tom.animationDuration + 1.0;
[self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay];
}
- (IBAction)drink {
[self runAnimationWithCount:81 name:@"drink"];
}
- (IBAction)knock {
[self runAnimationWithCount:81 name:@"knockout"];
}
- (IBAction)rightFoot {
[self runAnimationWithCount:30 name:@"footRight"];
}
链接: http://pan.baidu.com/s/1c0jQP4c 密码: f864