首页 > 代码库 > TOM猫

TOM猫


===

#import "CHViewController.h"

@interface CHViewController ()

@end

@implementation CHViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}
- (void) playAnimation:(int)count filename:(NSString *)filename{
    //创建可变数组
    NSMutableArray *imagesArray = [[NSMutableArray alloc] init];
    //添加图片
    /*加载图片缓存问题:
     1.有缓存(无法释放,参数传的是文件名)
     [UIImage imageNamed:@""];//经常使用的可以用这个方法
     2.无缓存(用完就会释放,参数传的是全路径)
     [[UIImage alloc] initWithContentsOfFile:path];//占用内存大的不经常使用的用这个
     */
    for(int i=0;i<count;i++){
        NSString *name = [NSString stringWithFormat:@"%@_%02d.jpg",filename,i];
//        UIImage *image = [UIImage imageNamed:name];//有缓存
        NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
        UIImage *img = [[UIImage alloc] initWithContentsOfFile:path];//无缓存
        [imagesArray addObject:img];
    }
    //动画效果
    _tom.animationImages = imagesArray;
    _tom.animationDuration = 0.1 * count;
    _tom.animationRepeatCount = 1;
    [_tom startAnimating];
}
    //找到配置文件tom.plist的路径
- (IBAction)btnClick:(UIButton *)sender {
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *path = [bundle pathForResource:@"tom" ofType:@"plist"];
    //根据文件路径加载字典
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
    //获取按钮中的title值
    NSString * title = [sender titleForState:UIControlStateNormal];
    //获取字典中事件对应的图片count
    int count = [dict[title] intValue];
    NSLog(@"%@-%d",title,count);
    [self playAnimation:count filename:title];
}
@end