标签:ios
最终效果图:
BeyondViewController.h
// // BeyondViewController.h // 05_TomCat // // Created by beyond on 14-7-23. // Copyright (c) 2014年 com.beyond. All rights reserved. // #import <UIKit/UIKit.h> @interface BeyondViewController : UIViewController @property (weak, nonatomic) IBOutlet UIImageView *imgView_tom; - (IBAction)btnClick:(UIButton *)sender; @end
BeyondViewController.m
// // BeyondViewController.m // 05_TomCat // // Created by beyond on 14-7-23. // Copyright (c) 2014年 com.beyond. All rights reserved. // #import "BeyondViewController.h" @interface BeyondViewController () { NSDictionary *_dict; } @end @implementation BeyondViewController - (void)viewDidLoad { [super viewDidLoad]; // sg_bundle模板代码,1,获得.app主要的包;2,返回主要的包中某个文件的fullPath全路径 NSBundle *mainBundle = [NSBundle mainBundle]; NSString *fullPath = [mainBundle pathForResource:@"tom.plist" ofType:nil]; _dict = [NSDictionary dictionaryWithContentsOfFile:fullPath]; } - (IBAction)btnClick:(UIButton *)sender { // 如果正在动画,则直接返回 if ([_imgView_tom isAnimating]) { return; } // 按钮上面的text就是字典的key,对应值-1是对应动画的图片张数 // NSString *text = sender.titleLabel.text; NSString *text = [sender titleForState:UIControlStateNormal]; int *picNum = [_dict[text] intValue]; NSLog(@"%@----%d",text,picNum); // 调用自定义方法 播放动画 [self playAnimatitonNamed:text picNumber:picNum]; } -(void)playAnimatitonNamed:(NSString *)name picNumber:(int)picNum { // 实例化可变数组 NSMutableArray *array = [NSMutableArray array]; // 向可变数组添加UIImage for (int i=0; i<picNum; i++) { // 生成image方式1,驻留内存,压力过大,真机会崩掉 NSString *fullName = [NSString stringWithFormat:@"%@_%02d.jpg",name,i]; UIImage *img = [UIImage imageNamed:fullName]; // 生成image方式2,IO读取全路径,用完就释放,不驻留内存 //NSString *fullPath = [[NSBundle mainBundle] pathForResource:fullName ofType:nil]; //UIImage *img2 = [[UIImage alloc] initWithContentsOfFile:fullPath]; // 添加UIImage到数组 [array addObject:img]; } // 设置UIImageView的动画参数,并提交动画 _imgView_tom.animationImages = array; _imgView_tom.animationDuration = 0.1*picNum; _imgView_tom.animationRepeatCount = 1; [_imgView_tom startAnimating]; } @end
标签:ios
原文地址:http://blog.csdn.net/pre_eminent/article/details/38072365