#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)drink;
- (IBAction)tou;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.imageView.image = [UIImage imageNamed:@"angry_00.jpg"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)anAnimating:(int)num name:(NSString *)name{
if(self.imageView.isAnimating) return;
NSMutableArray *images = [NSMutableArray array];
//for循环读出所有的的文件名
for (int i = 0; i < num; i++) {
//占位符的妙用,%2d 表示占两位,不足的用空格补上
//%02d表示占两位不足的用0补上
NSString *filename = [NSString stringWithFormat:@"%@_%02d.jpg",name,i];
//有缓存
// UIImage *image = [UIImage imageNamed:filename];
//获得文件的全路径
//没有缓存
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:filename ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:path];
[images addObject:image];
}
NSLog(@"%@",images);
//imageView的动态方法animationImages加载动画
//animationRepeatCount动画执行的趟数
//animationDuration动画的延迟时间
//startAnimating动画开始
//- (void)stopAnimating;停止动画
//- (BOOL)isAnimating;动画是否在执行
//imageview占内存
self.imageView.animationImages = images;
self.imageView.animationRepeatCount = 1;
self.imageView.animationDuration = images.count*0.05;
[self.imageView startAnimating];
//直接写这导致动画还没放完就清空了
//设置一个定时器
//self.imageView.animationImages = nil;
//设置定时器
//withObject 表示要传的参数
CGFloat delay = self.imageView.animationDuration + 1.0;
[self performSelector:@selector(clearCache) withObject:nil afterDelay:delay];
}
- (IBAction)drink {
//knockout
[self anAnimating:81 name:@"drink"];
}
- (void)clearCache
{
self.imageView.animationImages = nil;
}
//解决缓存问题,当上一个动画没有执行完,会把上一个动画放到缓存区,占用资源,解决这个问题就是释放上次的动作资源
//缓存的好处就是速度快
- (IBAction)tou {
[self anAnimating:81 name:@"knockout"];
}
@end
转载于:https://www.cnblogs.com/wohaoxue/p/4738396.html