主要代码如下:
//判断如果正在执行动画,则直接返回
if (self.imageV.isAnimating) {
return;
}
//定义一个可变数组
NSMutableArray *arrayM = [[NSMutableArray alloc]init];
for (int i = 0; i<count; i++) {
//拼接图片名称
NSString *imgName = [NSString stringWithFormat:@"%@_%.2d.jpg",picName,i];
//获取图片
NSString *path = [[NSBundle mainBundle] pathForResource:imgName ofType:nil];
UIImage *img = [UIImage imageWithContentsOfFile:path];
// UIImage *img = [UIImage imageNamed:imgName];
//将图片添加到可变数组
[arrayM addObject:img];
}
//设置UIImageView的animationImages属性,属性中包含执行动画所需的图片
self.imageV.animationImages = arrayM;
//设置动画持续时间
self.imageV.animationDuration = self.imageV.animationImages.count*0.1;
//设置动画重复次数
self.imageV.animationRepeatCount = 1;
//开始动画
[self.imageV startAnimating];
CGFloat delay = self.imageV.animationDuration;
[self performSelector:@selector(clearCache) withObject:nil afterDelay:delay];
}
- (IBAction)knockHead:(UIButton *)sender {
[self startAnimating:81 picName:@"knockout"];
}
- (IBAction)eat:(UIButton *)sender {
[self startAnimating:40 picName:@"eat"];
}
- (IBAction)fart:(UIButton *)sender {
[self startAnimating:28 picName:@"fart"];
}
- (IBAction)cymbal:(UIButton *)sender {
[self startAnimating:13 picName:@"cymbal"];
}
- (IBAction)drink:(UIButton *)sender {
[self startAnimating:81 picName:@"drink"];
}