码迷,mamicode.com
首页 > 移动开发 > 详细

iOS_5_汤姆猫

时间:2014-07-24 10:42:58      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:ios

最终效果图:

bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣

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_5_汤姆猫

标签:ios

原文地址:http://blog.csdn.net/pre_eminent/article/details/38072365

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!