iOS开发之Tom猫

本文介绍了一种在iOS应用中优化动画加载的方法,通过在viewDidLoad中预加载资源包,避免了每次触发动画时重复加载资源,提升了用户体验。同时,详细展示了如何使用NSBundle和NSMutableDictionary来管理动画资源和配置动画参数。

@interfaceViewController ()

 

@end

//定义一个全局的可变数据字典

NSMutableDictionary *dictionary;

@implementation ViewController

 

- (void)viewDidLoad

{

    [superviewDidLoad];

//在viewDidLoad加载资源包,避免每次点击时都需要加载资源包,

    NSBundle *bundle = [NSBundle mainBundle];

    NSString *path = [bundle pathForResource:@"tom" ofType:@"plist"];

    dictionary = [NSMutableDictionarydictionaryWithContentsOfFile:path];

 

// Do any additional setup after loading the view, typically from a nib.

}

 

- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

-(void)setCount:(int)count setName:(NSString *)Name

{

// 定义了一个可变数组,用来存放照片

      NSMutableArray *images = [NSMutableArrayarray];

  for (int i=0; i<count; i++) {

        NSString *name = [NSString stringWithFormat:@"%@_%02d.jpg",Name,i];

//        UIImage *img = [UIImage imageNamed:name];

/*通过调用[[NSBundle mainBundle]pathForResource:   ofType:  ] 

  和[[UIImage alloc]initWithContentsOfFile:path],这两句,可以避免咱用缓存不释放

*/

        NSString *path = [[NSBundle mainBundle]pathForResource:name ofType:nil];

        UIImage *imagesname = [[UIImage alloc]initWithContentsOfFile:path];

        [images addObject:imagesname];

    }

        _tom.animationImages = images;

        _tom.animationRepeatCount = 1;

        _tom.animationDuration = count*0.1;

       [_tomstartAnimating];

        

 

}

- (IBAction)btnClik:(UIButton *)sender {

    if ([_tomisAnimating])

        return;

    NSString *title = [sender titleForState:UIControlStateNormal];

    int count = [dictionary[title]intValue];

    [self setCount:count setName:title];

    

}

@end

 

转载于:https://www.cnblogs.com/caishugeng/p/3741304.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值