博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UI基础--手写代码实现汤姆猫动画
阅读量:5290 次
发布时间:2019-06-14

本文共 4465 字,大约阅读时间需要 14 分钟。

////  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

你可能感兴趣的文章
解决java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Date
查看>>
将.lib库文件转换成.a库文件的工具
查看>>
FZU 2129 子序列个数 (动态规划)
查看>>
20155324 2016-2017-2 《Java程序设计》第7周学习总结
查看>>
CSS清浮动处理(Clear与BFC)
查看>>
thinkphp路由
查看>>
HDU - 1248-寒冰王座
查看>>
angular OnChange事件
查看>>
owin Oauth
查看>>
java String 强化操作 判断数字 字符串转阿拉伯数字,相似度等等
查看>>
Win(Phone)10开发第(5)弹,本地媒体服务器的一些注意事项
查看>>
[HDU5536] Chip Factory
查看>>
kettle转换和作业插件开发及调试
查看>>
npm run dev时报错“events.js:160 throw er; // Unhandled 'error' event”
查看>>
[java学习笔记]freemarker模板的页面静态化
查看>>
WebAPI使用多个xml文件生成帮助文档
查看>>
Android 获取手机本机内存、SD卡内存使用情况
查看>>
Use Drozer To Find SQL Inject In Diva Apk
查看>>
Qt中delete的问题
查看>>
Json数据格式
查看>>