IOS 汤姆猫核心代码

来源:互联网 发布:网络网页优化方案 编辑:程序博客网 时间:2024/05/16 19:27
////  MJViewController.m//  03-Tom////  Created by apple on 13-11-24.//  Copyright (c) 2013年 itcast. All rights reserved.//#import "MJViewController.h"@interface MJViewController () {    NSDictionary *_dict; // 保存所有图片的个数}@end@implementation MJViewController- (void)viewDidLoad {    [super viewDidLoad];        // 1.获得tom.plist的全路径    NSBundle *bundle = [NSBundle mainBundle];    NSString *path = [bundle pathForResource:@"tom" ofType:@"plist"];        // 2.根据文件路径加载字典    _dict = [NSDictionary dictionaryWithContentsOfFile:path];}- (void)playAnim:(int)count fliename:(NSString *)filename {    // 1.创建可变数组    NSMutableArray *images = [NSMutableArray array];        // 2.添加图片    for (int i = 0; i<count; i++) {        // 图片名        NSString *name = [NSString stringWithFormat:@"%@_%02d.jpg", filename, i];        // 全路径        NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];                // 加载图片(缓存)//        UIImage *img = [UIImage imageNamed:name];        // 没有缓存        UIImage *img = [[UIImage alloc] initWithContentsOfFile:path];                [images addObject:img];    }        // 3.设置动画图片(有顺序)    _tom.animationImages = images;// 序列帧动画        // 4.只播放一次    _tom.animationRepeatCount = 1;        // 5.设置动画的持续时间    _tom.animationDuration = 0.1 * count;        // 5.开始动画    [_tom startAnimating];}#pragma mark 监听所有的按钮点击- (IBAction)btnClick:(UIButton *)sender {    // 1.如果tom正在播放动画,直接返回    if (_tom.isAnimating) return;        // 2.取出按钮文字    NSString *title = [sender titleForState:UIControlStateNormal];        // 3.获得图片数量    int count = [_dict[title] intValue];        // 4.播放动画    [self playAnim:count fliename:title];}@end

  

 

0 0
原创粉丝点击