A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wwf707542865 中级黑马   /  2015-11-2 23:35  /  2975 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *animationImageView;

@end

@implementation ViewController
-(NSMutableArray *)imgDate:(NSInteger)senderTag{
    //定义一个数组,用来存储图片的数量
    NSArray *array=[NSArray array];
    array=@[@26,@13,@81,@40,@28,@30,@30,@81,@24,@56,@34];//老师提供的素材对应动作的图片数量
   
    //定义数组存放字符串,用来作为图片名的前半部分,拼接图片名
    NSArray *strArr=[NSArray array];
    strArr=@[@"angry",@"cymbal",@"drink",@"eat",@"fart",@"footLeft",@"footRight",
    @"knockout",@"pie",@"scratch",@"stomach"];

    //定义一个数组,用来存放加载的动作的图片
    NSMutableArray *imgArray=[NSMutableArray array];
    //通过sender.tag来获取图片资源
        for (int i=0; i<[array[senderTag] intValue]; i++) {
            NSString *stringImageName=[NSString stringWithFormat:@"%@_%02d",strArr[senderTag],i];
         
            //下面的方法比较耗费内存,因为imageNamed加载图片到内存之后,在内存中不会自动被释放,图片加载过多就造成内存占用过大
            //UIImage *img=[UIImage imageNamed:stringImageName];

            //解决类存过大方法如下
            NSBundle *bundle=[NSBundle mainBundle];
           // NSLog(@"%@",bundle);
            //获取文件路径
            NSString *strPath=[bundle pathForResource:stringImageName ofType:@"jpg"];
            //从一个路径获取图片
            //事实上,经过系统处理,图片被加载到了/Users/mac/Library/Developer/CoreSimulator/Devices/C6BECAE3-5586-4E39-A86D-BE981FDED53C/data/Containers/Bundle/Application/CD1E7E49-66DF-47CF-806C-BE2FF577BB59目录下,但是被包装成了.car文件,无法被正常读取
  //此时,我们可以创建一个虚拟文件夹,就是将图片拷贝到跟目录下,使用copy groups选项创建,用来存放这些图片
            
       UIImage *img=[UIImage imageWithContentsOfFile:strPath]; //将图片加入图片数组
            [imgArray addObject:img];//将图片加入数组
    }
    return imgArray;//返回这个对应senderTag的数组,也就是通过按钮的tag值获取对应的动作图片数组
}
-(IBAction)startAnimation:(UIButton *)sender{
    //动画不允许被打断,判断isAnimation
    if (self.animationImageView.isAnimating) {
        return;
    }
//获取sender.tag对应的图片数组
    self.animationImageView.animationImages=[self imgDate:sender.tag];
  
    //控制动画执行次数
    self.animationImageView.animationRepeatCount=1;
    //控制动画执行时间
    self.animationImageView.animationDuration=[self imgDate:sender.tag].count/12;//12张图片一秒
    //开始动画
    [self.animationImageView startAnimating];
    //动画结束之后,释放内存
    //此处的(setAnimationImages:)是imageView空间自带的set方法
    [self.animationImageView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.animationImageView.animationDuration+0.01];
}

8 个回复

倒序浏览
如果只是实现功能而不去考虑内存和动画打断,代码可能只需要30行搞定
回复 使用道具 举报
学习了,以前一直不懂,现在懂一点点了
回复 使用道具 举报
赞一个、~
回复 使用道具 举报
wwf707542865 发表于 2015-11-2 23:37
如果只是实现功能而不去考虑内存和动画打断,代码可能只需要30行搞定

好厉害的样子。{:2_32:}
回复 使用道具 举报
好想很厉害啊,等以后学的多了,好好看看
回复 使用道具 举报
cc_2436 中级黑马 2015-11-23 22:23:12
7#
666666666666666
回复 使用道具 举报
赞赞赞!!!
回复 使用道具 举报
谢谢分享,看了看收获还不少
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马