UI基础--手写代码实现汤姆猫动画

//
//  ViewController.m
//  TomCat
//
//  Created by xiaomoge on 14/12/10.
//  Copyright (c) 2014年 xiaomoge. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    UIButton *_btnDrink;// 喝动作按钮
    UIButton *_btnEat;// 吃动作按钮
    UIButton *_btnFart;// 放屁动作按钮
    UIButton *_btnPie;// 馅饼动作按钮
    UIButton *_btnScratch;// 抓动作按钮
    UIButton *_btnCymbal;// 钹动作按钮
    UIButton *_btnKnockOut;// 敲头动作按钮
    UIButton *_btnFootLeft;// 左脚动作按钮
    UIButton *_btnFootRight;// 右脚动作按钮
    UIButton *_btnStomach;// 腹部动作按钮
    UIImageView *_imageView;// 图片
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImage *image = [UIImage imageNamed:@"drink_00.jpg"];
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    _imageView.image = image;
    [self.view addSubview:_imageView];
    _btnKnockOut = [self creatBtn:CGRectMake(70, 75, 190, 160) andImage:nil andSEL:@selector(knockout)];
    _btnStomach = [self creatBtn:CGRectMake(100, 310, 120, 90) andImage:nil andSEL:@selector(stomach)];
    _btnFootLeft = [self creatBtn:CGRectMake(110, 440, 40, 30) andImage:nil andSEL:@selector(footLeft)];
    _btnFootRight = [self creatBtn:CGRectMake(170, 420, 50, 30) andImage:nil andSEL:@selector(footRight)];
    _btnDrink = [self creatBtn:CGRectMake(20, 400, 60, 60) andImage:[UIImage imageNamed:@"drink"] andSEL:@selector(drink)];
    _btnEat = [self creatBtn:CGRectMake(20, 330, 60, 60) andImage:[UIImage imageNamed:@"eat"] andSEL:@selector(eat)];
    _btnFart = [self creatBtn:CGRectMake(20, 260, 60, 60) andImage:[UIImage imageNamed:@"fart"] andSEL:@selector(fart)];
    _btnPie = [self creatBtn:CGRectMake(250, 260, 60, 60) andImage:[UIImage imageNamed:@"pie"] andSEL:@selector(pie)];
    _btnCymbal = [self creatBtn:CGRectMake(250, 330, 60, 60) andImage:[UIImage imageNamed:@"cymbal"] andSEL:@selector(cymbal)];
    _btnScratch = [self creatBtn:CGRectMake(250, 400, 60, 60) andImage:[UIImage imageNamed:@"scratch"] andSEL:@selector(scratch)];
}
#pragma mark - 创建一个button按钮
- (UIButton *)creatBtn:(CGRect)fram andImage:(UIImage *)image andSEL:(SEL)sel{
    UIButton *btn = [[UIButton alloc] init];
    btn.frame = fram;
    [btn setBackgroundImage:image forState:UIControlStateNormal];
    [btn addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    return btn;
}
#pragma  mark - 执行动画的事件
- (void)animationOfAction:(NSString *)name andCount:(NSInteger)count{
    
    if (_imageView.isAnimating) return;// 判断是否正在执行动画,如果是就直接退出,否就执行下一句
    NSMutableArray *images = [NSMutableArray arrayWithCapacity:count];// 创建一个接收需要执行帧动画的图片数组
    for (int index = 0; index < count; index++) {
        NSString *imagesName = [NSString stringWithFormat:@"%@_%02d",name,index];// 根据索引生成图片名字
        NSString *path = [[NSBundle mainBundle] pathForResource:imagesName ofType:@"jpg"];// 获取图片的所在路径
//       创建图片
//      imageNamed 带有缓存,通过imageNamed创建的图片会放到缓存中,执行动画时内存会过高,所以为了内存优化着想,不推荐使用imageNamed创建的图片
//        当你把图片放在 Images.xcassets 就只能通过imageNamed加载
//        UIImage  *image = [UIImage imageNamed:imagesName];
        UIImage *image = [UIImage imageWithContentsOfFile:path];// 从图片的路径获得图片,这里图片的路径放在Supporting Files下
        [images addObject:image];//把图片加入到帧动画数组中
    }
    [_imageView setAnimationImages:images];// 设置执行帧动画的图片数组
    _imageView.animationDuration = count * 0.05;//帧动画执行的时间
    _imageView.animationRepeatCount = 1;// 帧动画执行的次数
    [_imageView startAnimating];// 开始动画
    [_imageView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:_imageView.animationDuration + 1];// 在帧动画执行完1秒后清除缓存的图片
}
#pragma mark - 腹部
- (void)stomach{
    [self animationOfAction:@"stomach" andCount:34];
}
#pragma mark - 左脚
- (void)footLeft{
    [self animationOfAction:@"footLeft" andCount:30];
}
#pragma mark - 右脚
- (void)footRight{
    [self animationOfAction:@"footRight" andCount:30];
}
#pragma mark - 敲头
- (void)knockout{
    [self animationOfAction:@"knockout" andCount:81];
}
#pragma mark - 喝水
- (void)drink{
    [self animationOfAction:@"drink" andCount:81];
}
#pragma mark - 吃
- (void)eat{
    [self animationOfAction:@"eat" andCount:39];
}
#pragma mark - 放屁
- (void)fart{
    [self animationOfAction:@"fart" andCount:28];
}
#pragma mark - 馅饼
- (void)pie{
    [self animationOfAction:@"pie" andCount:24];
}
#pragma mark - 抓
- (void)scratch{
    [self animationOfAction:@"scratch" andCount:56];
}
#pragma mark - 钹
- (void)cymbal{
    [self animationOfAction:@"cymbal" andCount:13];
}
@end
原文地址:https://www.cnblogs.com/xiaomoge/p/4158556.html