汤姆猫—帧动画

#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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值