Tom猫
//
// ViewController.m
// TomCat2
//
//
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *tom;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *bundlePath = [[[NSBundle mainBundle] bundlePath]stringByAppendingPathComponent:@"ainimals"];
NSString *filePath = [NSString stringWithFormat:@"%@/Drink/drink_00.jpg",bundlePath];
self.tom.image = [UIImage imageWithContentsOfFile:filePath];
}
- (void)runAnimationWithName:(NSString *)name
{
if (self.tom.isAnimating) return;
// 1.设置Tom的动画数组
NSMutableArray *arrayM = [NSMutableArray array];
NSString *bundlePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"ainimals"];
NSString *animPath = [bundlePath stringByAppendingPathComponent:name];
// NSFileManager是专门用来做磁盘文件管理的
// 取出指定目录中的所有文件
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:animPath error:NULL];
NSLog(@"%@",files);
for (NSString *fileName in files) {
NSString *filePath = [NSString stringWithFormat:@"%@/%@",animPath,fileName];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
[arrayM addObject:image];
}
self.tom.animationImages = arrayM;
self.tom.animationDuration = arrayM.count * 0.075;
self.tom.animationRepeatCount = 1;
// 2.开始播放
[self.tom startAnimating];
NSLog(@"%f",self.tom.animationDuration);
// 3.清空动画数组
[self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration];
}
-(IBAction)tomAction:(UIButton *)button
{
[self runAnimationWithName:button.currentTitle];
}
@end
下面是全代码实现
//
// ViewController.m
// TomCat全代码实现
#import "ViewController.h"
@interface ViewController ()
/** tom图片 */
@property (nonatomic,strong) UIImageView *tom;
/** 猫头 */
@property (nonatomic,strong) UIButton *head;
/** 猫肚子 */
@property (nonatomic,strong) UIButton *stomach;
/** 猫XX */
@property (nonatomic,strong) UIButton *jj;
/** 左脚 */
@property (nonatomic,strong) UIButton *leftFoot;
/** 右脚 */
@property (nonatomic,strong) UIButton *rightFoot;
/** 敲锣 */
@property (nonatomic,strong) UIButton *cymbal;
/** 吃 */
@property (nonatomic,strong) UIButton *eat;
/** 喝 */
@property (nonatomic,strong) UIButton *drink;
/** 放屁 */
@property (nonatomic,strong) UIButton *fart;
/** 大饼子 */
@property (nonatomic,strong) UIButton *pie;
/** 抓痕 */
@property (nonatomic,strong) UIButton *scratch;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tom = [[UIImageView alloc]init];
self.tom.frame = self.view.frame;
NSLog(@"%@",NSStringFromCGRect(self.view.frame));
NSLog(@"%@",NSStringFromCGRect([[UIScreen mainScreen] bounds]));
NSString *bundlePath = [[[NSBundle mainBundle] bundlePath]stringByAppendingPathComponent:@"ainimals"];
NSString *filePath = [NSString stringWithFormat:@"%@/Drink/drink_00.jpg",bundlePath];
NSLog(@"%@",filePath);
self.tom.image = [UIImage imageWithContentsOfFile:filePath];
[self.view addSubview:self.tom];
self.head = [[UIButton alloc] init];
self.head.frame = CGRectMake(75, 102, 170, 170);
[self.head setTitle:@"Knockout" forState:UIControlStateNormal];
[self.head setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[self.head addTarget:self action:@selector(tomAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.head];
self.stomach = [[UIButton alloc] init];
self.stomach.frame = CGRectMake(130, 332, 60, 60);
[self.stomach setTitle:@"Stomach" forState:UIControlStateNormal];
[self.stomach setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[self.stomach addTarget:self action:@selector(tomAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.stomach];
self.jj = [[UIButton alloc] init];
self.jj.frame = CGRectMake(130, 434, 60, 60);
[self.jj setTitle:@"Angry" forState:UIControlStateNormal];
[self.jj setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[self.jj addTarget:self action:@selector(tomAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.jj];
self.leftFoot = [[UIButton alloc] init];
self.leftFoot.frame = CGRectMake(161, 510, 46, 40);
[self.leftFoot setTitle:@"FootLeft" forState:UIControlStateNormal];
[self.leftFoot setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[self.leftFoot addTarget:self action:@selector(tomAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.leftFoot];
self.rightFoot = [[UIButton alloc] init];
self.rightFoot.frame = CGRectMake(107, 510, 46, 40);
[self.rightFoot setTitle:@"FootRight" forState:UIControlStateNormal];
[self.rightFoot setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[self.rightFoot addTarget:self action:@selector(tomAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.rightFoot];
self.cymbal = [[UIButton alloc] init];
self.cymbal.frame = CGRectMake(16, 342, 55, 60);
[self.cymbal setTitle:@"Cymbal" forState:UIControlStateNormal];
[self.cymbal setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[self.cymbal setImage:[UIImage imageNamed:@"cymbal"] forState:UIControlStateNormal];
[self.cymbal addTarget:self action:@selector(tomAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.cymbal];
self.eat = [[UIButton alloc] init];
self.eat.frame = CGRectMake(16, 410, 55, 60);
[self.eat setTitle:@"Eat" forState:UIControlStateNormal];
[self.eat setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[self.eat setImage:[UIImage imageNamed:@"eat"] forState:UIControlStateNormal];
[self.eat addTarget:self action:@selector(tomAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.eat];
self.drink = [[UIButton alloc] init];
self.drink.frame = CGRectMake(16, 478, 55, 60);
[self.drink setTitle:@"Drink" forState:UIControlStateNormal];
[self.drink setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[self.drink setImage:[UIImage imageNamed:@"drink"] forState:UIControlStateNormal];
[self.drink addTarget:self action:@selector(tomAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.drink];
self.fart = [[UIButton alloc] init];
self.fart.frame = CGRectMake(249, 342, 55, 60);
[self.fart setTitle:@"Fart" forState:UIControlStateNormal];
[self.fart setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[self.fart setImage:[UIImage imageNamed:@"fart"] forState:UIControlStateNormal];
[self.fart addTarget:self action:@selector(tomAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.fart];
self.pie = [[UIButton alloc] init];
self.pie.frame = CGRectMake(249, 410, 55, 60);
[self.pie setTitle:@"Pie" forState:UIControlStateNormal];
[self.pie setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[self.pie setImage:[UIImage imageNamed:@"pie"] forState:UIControlStateNormal];
[self.pie addTarget:self action:@selector(tomAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.pie];
self.scratch = [[UIButton alloc] init];
self.scratch.frame = CGRectMake(249, 478, 55, 60);
[self.scratch setTitle:@"Scratch" forState:UIControlStateNormal];
[self.scratch setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[self.scratch setImage:[UIImage imageNamed:@"scratch"] forState:UIControlStateNormal];
[self.scratch addTarget:self action:@selector(tomAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.scratch];
}
- (void)runAnimationWithName:(NSString *)name
{
if (self.tom.isAnimating) return;
// 设置图片数组
NSMutableArray *arrayM = [NSMutableArray array];
// 拼接图片路径
NSString *bundlePath = [[[NSBundle mainBundle] bundlePath]stringByAppendingPathComponent:@"ainimals"];
NSString *animPath = [bundlePath stringByAppendingPathComponent:name];
// 将拼接好的路径中的图片放到数组中
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:animPath error:nil];
NSLog(@"%@",files);
// 遍历数组把图片一张张的放到可变数组中
for (NSString *fileName in files){
NSString *filePath = [NSString stringWithFormat:@"%@/%@",animPath,fileName];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
[arrayM addObject:image];
}
// 设置动画图片
self.tom.animationImages = arrayM;
// 设置播放时长大约每张0.075秒
self.tom.animationDuration = arrayM.count * 0.075;
// 设置动画播放循环次数为1次
self.tom.animationRepeatCount = 1;
// 开始动画
[self.tom startAnimating];
// 清空动画缓存
[self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration];
}
- (void)tomAction:(UIButton *)button
{
[self runAnimationWithName:button.currentTitle];
}
@end
下面是封装之后的懒加载代码
//
// ViewController.m
// TomCat全代码实现
//
// Created by 胡云鹏 on 15/3/23.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
/** tom图片 */
@property (nonatomic,strong) UIImageView *tom;
/** 猫头 */
@property (nonatomic,strong) UIButton *head;
/** 猫肚子 */
@property (nonatomic,strong) UIButton *stomach;
/** 猫XX */
@property (nonatomic,strong) UIButton *jj;
/** 左脚 */
@property (nonatomic,strong) UIButton *leftFoot;
/** 右脚 */
@property (nonatomic,strong) UIButton *rightFoot;
/** 敲锣 */
@property (nonatomic,strong) UIButton *cymbal;
/** 吃 */
@property (nonatomic,strong) UIButton *eat;
/** 喝 */
@property (nonatomic,strong) UIButton *drink;
/** 放屁 */
@property (nonatomic,strong) UIButton *fart;
/** 大饼子 */
@property (nonatomic,strong) UIButton *pie;
/** 抓痕 */
@property (nonatomic,strong) UIButton *scratch;
@end
@implementation ViewController
- (UIImageView *)tom
{
if (!_tom) {
_tom = [[UIImageView alloc]init];
_tom.frame = self.view.frame;
NSLog(@"%@",NSStringFromCGRect(self.view.frame));
NSLog(@"%@",NSStringFromCGRect([[UIScreen mainScreen] bounds]));
NSString *bundlePath = [[[NSBundle mainBundle] bundlePath]stringByAppendingPathComponent:@"ainimals"];
NSString *filePath = [NSString stringWithFormat:@"%@/Drink/drink_00.jpg",bundlePath];
NSLog(@"%@",filePath);
_tom.image = [UIImage imageWithContentsOfFile:filePath];
[self.view addSubview:_tom];
}
return _tom;
}
- (UIButton *)head
{
if (!_head) {
_head = [[UIButton alloc] init];
_head.frame = CGRectMake(75, 102, 170, 170);
[_head setTitle:@"Knockout" forState:UIControlStateNormal];
[_head setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[_head addTarget:self action:@selector(tomAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_head];
}
return _head;
}
- (UIButton *)stomach
{
if (!_stomach) {
_stomach = [[UIButton alloc] init];
_stomach.frame = CGRectMake(130, 332, 60, 60);
[_stomach setTitle:@"Stomach" forState:UIControlStateNormal];
[_stomach setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[_stomach addTarget:self action:@selector(tomAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_stomach];
}
return _stomach;
}
- (UIButton *)jj
{
if (!_jj) {
_jj = [[UIButton alloc] init];
_jj.frame = CGRectMake(130, 434, 60, 60);
[_jj setTitle:@"Angry" forState:UIControlStateNormal];
[_jj setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[_jj addTarget:self action:@selector(tomAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_jj];
}
return _jj;
}
- (UIButton *)leftFoot
{
if (!_leftFoot) {
_leftFoot = [[UIButton alloc] init];
_leftFoot.frame = CGRectMake(161, 510, 46, 40);
[_leftFoot setTitle:@"FootLeft" forState:UIControlStateNormal];
[_leftFoot setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[_leftFoot addTarget:self action:@selector(tomAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_leftFoot];
}
return _leftFoot;
}
- (UIButton *)rightFoot
{
if (!_rightFoot) {
_rightFoot = [[UIButton alloc] init];
_rightFoot.frame = CGRectMake(107, 510, 46, 40);
[_rightFoot setTitle:@"FootRight" forState:UIControlStateNormal];
[_rightFoot setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[_rightFoot addTarget:self action:@selector(tomAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_rightFoot];
}
return _rightFoot;
}
- (UIButton *)cymbal
{
if (!_cymbal) {
_cymbal = [[UIButton alloc] init];
_cymbal.frame = CGRectMake(16, 342, 55, 60);
[_cymbal setTitle:@"Cymbal" forState:UIControlStateNormal];
[_cymbal setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[_cymbal setImage:[UIImage imageNamed:@"cymbal"] forState:UIControlStateNormal];
[_cymbal addTarget:self action:@selector(tomAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_cymbal];
}
return _cymbal;
}
- (UIButton *)eat
{
if (!_eat) {
_eat = [[UIButton alloc] init];
_eat.frame = CGRectMake(16, 410, 55, 60);
[_eat setTitle:@"Eat" forState:UIControlStateNormal];
[_eat setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[_eat setImage:[UIImage imageNamed:@"eat"] forState:UIControlStateNormal];
[_eat addTarget:self action:@selector(tomAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_eat];
}
return _eat;
}
- (UIButton *)drink
{
if (!_drink) {
_drink = [[UIButton alloc] init];
_drink.frame = CGRectMake(16, 478, 55, 60);
[_drink setTitle:@"Drink" forState:UIControlStateNormal];
[_drink setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[_drink setImage:[UIImage imageNamed:@"drink"] forState:UIControlStateNormal];
[_drink addTarget:self action:@selector(tomAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_drink];
}
return _drink;
}
- (UIButton *)fart
{
if (!_fart) {
_fart = [[UIButton alloc] init];
_fart.frame = CGRectMake(249, 342, 55, 60);
[_fart setTitle:@"Fart" forState:UIControlStateNormal];
[_fart setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[_fart setImage:[UIImage imageNamed:@"fart"] forState:UIControlStateNormal];
[_fart addTarget:self action:@selector(tomAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_fart];
}
return _fart;
}
- (UIButton *)pie
{
if (!_pie) {
_pie = [[UIButton alloc] init];
_pie.frame = CGRectMake(249, 410, 55, 60);
[_pie setTitle:@"Pie" forState:UIControlStateNormal];
[_pie setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[_pie setImage:[UIImage imageNamed:@"pie"] forState:UIControlStateNormal];
[_pie addTarget:self action:@selector(tomAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_pie];
}
return _pie;
}
- (UIButton *)scratch
{
if (!_scratch) {
_scratch = [[UIButton alloc] init];
_scratch.frame = CGRectMake(249, 478, 55, 60);
[_scratch setTitle:@"Scratch" forState:UIControlStateNormal];
[_scratch setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[_scratch setImage:[UIImage imageNamed:@"scratch"] forState:UIControlStateNormal];
[_scratch addTarget:self action:@selector(tomAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_scratch];
}
return _scratch;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self tom];
[self head];
[self stomach];
[self jj];
[self leftFoot];
[self rightFoot];
[self cymbal];
[self eat];
[self drink];
[self fart];
[self pie];
[self scratch];
}
- (void)runAnimationWithName:(NSString *)name
{
if (self.tom.isAnimating) return;
// 设置图片数组
NSMutableArray *arrayM = [NSMutableArray array];
// 拼接图片路径
NSString *bundlePath = [[[NSBundle mainBundle] bundlePath]stringByAppendingPathComponent:@"ainimals"];
NSString *animPath = [bundlePath stringByAppendingPathComponent:name];
// 将拼接好的路径中的图片放到数组中
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:animPath error:nil];
NSLog(@"%@",files);
// 遍历数组把图片一张张的放到可变数组中
for (NSString *fileName in files){
NSString *filePath = [NSString stringWithFormat:@"%@/%@",animPath,fileName];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
[arrayM addObject:image];
}
// 设置动画图片
self.tom.animationImages = arrayM;
// 设置播放时长大约每张0.075秒
self.tom.animationDuration = arrayM.count * 0.075;
// 设置动画播放循环次数为1次
self.tom.animationRepeatCount = 1;
// 开始动画
[self.tom startAnimating];
// 清空动画缓存
[self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration];
}
- (void)tomAction:(UIButton *)button
{
[self runAnimationWithName:button.currentTitle];
}
@end