#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
- (IBAction)eat;
- (IBAction)drink;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *btn =[[UIButton alloc] initWithFrame:CGRectMake(90, 120, 190, 190)];
//btn.backgroundColor=[UIColor redColor];
[btn setTitle:nil forState:UIControlStateNormal];
[self.view addSubview:btn];
//为按钮添加绑定事件
/*
第一个参数:交给谁进行处理,一般是控制器
第二个参数 :处理的时候调用控制器的那一个方法
第三个参数 :事件类型,为那一个事件添加
*/
[btn addTarget:self action:@selector(headTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
}
- (void) headTouchUpInside
{
//严格区分大小写
[self tomCatAnimation:@"knockout" AndCount:81];
}
- (IBAction)eat {
[self tomCatAnimation:@"eat" AndCount:40];
}
//封闭tom动画
- (void) tomCatAnimation:(NSString *)name AndCount:(int) cnt
{
//判断当前是否正在进行某个动画,如果是,则不处理当前 的点击操作
if (self.iconView.isAnimating) { //如果 在动画正在进行,则不进行处理
return;
}
//1.准备UIImage对象数组
NSMutableArray *imgArr=[NSMutableArray array];
//循环添加数据到数组
for (int i=0; i<cnt; i++) {
//1.生成文件名
NSString *imgName=[NSString stringWithFormat:@"%@_%02d.jpg",name,i];
//2.通过文件名创建UIImage对象:这个方法会生成缓存,意味着它会将占据的资源一直保留在内存中,只到应用程序退出
//UIImage *img=[UIImage imageNamed:imgName];
//imageWithContentsOfFile:需要全路径
NSBundle *bundle=[NSBundle mainBundle];
//获取指定名称的图像的全路径
NSString *finalPath=[bundle pathForResource:imgName ofType:nil];
// NSLog(@"%@",imgName);
// NSLog(@"%@",finalPath);
//根据全路径创建UIImage对象,它不会生成缓存,下一次动画播放的时候会将上一次的资源释放
UIImage *img=[UIImage imageWithContentsOfFile:finalPath];
//添加图片对象到数组
[imgArr addObject:img];
}
//2.指定UIImageView动画所描述的数组--这个数组必须是UIImage对象数组
self.iconView.animationImages=imgArr;
//3.设置动画播放次数
[self.iconView setAnimationRepeatCount:1];
//4.设置动画播放时间
[self.iconView setAnimationDuration:cnt*0.08];
//5.开始动画
[self.iconView startAnimating];
//6.动画播放完毕之后,释放当前动画所使用的UIImage数组
//这句代码需要延迟执行:等动画执行完毕之后再进行代码的执行
//细节:调用谁的方法,就使用相应的对象调用performSelector
//[self performSelector:@selector(clearImg) withObject:nil afterDelay:cnt*0.08+1];
//[self performSelector:@selector(clearImg) withObject:nil afterDelay:cnt*0.08+1];
[self.iconView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:cnt*0.08];
}
- (void) clearImgfffffffffffffffffffffff
{
//self.iconView.animationImages=nil;
[self.iconView setAnimationImages:nil];
}
- (IBAction)drink {
[self tomCatAnimation:@"drink" AndCount:81];
// //1.准备UIImage对象数组
// NSMutableArray *imgArr=[NSMutableArray array];
// //2循环创建UIImage对象,添加到数组中
// for (int i=0; i<81; i++) {
// NSString *fileName=[NSString stringWithFormat:@"drink_%02d",i];
// //我们需要通过文件名创建UIImage对象
// UIImage *img=[UIImage imageNamed:fileName];
// //添加对象到数组
// [imgArr addObject:img];
// }
// //2.指定UIImageView动画所描述的数组--这个数组必须是UIImage对象数组
// self.iconView.animationImages=imgArr;
// //3.设置动画播放次数
// [self.iconView setAnimationRepeatCount:1];
// //4.设置动画播放时间
// [self.iconView setAnimationDuration:81*0.08];
// //5.开始动画
// [self.iconView startAnimating];
}
@end