博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
纯代码Tom
阅读量:5291 次
发布时间:2019-06-14

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

1 //  2 //  LWTViewController.m  3 //  纯代码Tom  4 //  5 //  Created by apple on 14-5-21.  6 //  Copyright (c) 2014年 lwt. All rights reserved.  7 //  8   9 #import "LWTViewController.h" 10 #define KBtnSize 60 11  12 // 图标按钮的左右的排列 13 typedef enum { 14     KButtonThird = 1, 15     KButtonSecond, 16     KButtonFirst 17 }KButton; 18  19 @interface LWTViewController () 20  21 // 保存plist数据 22 @property (nonatomic, strong) NSDictionary *dict; 23  24 // 全屏图片属性 25 @property (nonatomic, strong) UIImageView *tom; 26  27 @end 28  29 @implementation LWTViewController 30  31 // 懒加载plist文件 32 - (NSDictionary *)dict 33 { 34     if(!_dict) 35     { 36         NSBundle *bundle = [NSBundle mainBundle]; 37         NSString *dictPath = [bundle pathForResource:@"Tom" ofType:@"plist"]; 38         self.dict = [NSDictionary dictionaryWithContentsOfFile:dictPath]; 39     } 40     return _dict; 41 } 42  43 - (void)viewDidLoad 44 { 45     [super viewDidLoad]; 46     // Do any additional setup after loading the view, typically from a nib. 47      48     // 创建默认全屏图片View 49     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)]; 50      51     // 加载图片 52     NSBundle *bundle = [NSBundle mainBundle]; 53     NSString *imgUrl = [bundle pathForResource:@"angry_00.jpg" ofType:nil]; 54     UIImage *img = [[UIImage alloc] initWithContentsOfFile:imgUrl]; 55      56     // 将图片给View 57     imageView.image = img; 58     [self.view addSubview:imageView]; 59     self.tom = imageView; 60      61     // 创建点击按钮 62     // 左侧按钮 63     // eat 64     [self createBtn:@"eat" andCGRect:CGRectMake(0, (self.view.bounds.size.height - KBtnSize * KButtonFirst), KBtnSize, KBtnSize)]; 65     // cymbal 66     [self createBtn:@"cymbal" andCGRect:CGRectMake(0, (self.view.bounds.size.height - KBtnSize * KButtonSecond), KBtnSize, KBtnSize)]; 67     // drink 68     [self createBtn:@"drink" andCGRect:CGRectMake(0, (self.view.bounds.size.height - KBtnSize * KButtonThird), KBtnSize, KBtnSize)]; 69     // 右侧按钮 70     // fart 71     [self createBtn:@"fart" andCGRect:CGRectMake((self.view.bounds.size.width - KBtnSize), (self.view.bounds.size.height - KBtnSize * KButtonFirst), KBtnSize, KBtnSize)]; 72     // pie 73     [self createBtn:@"pie" andCGRect:CGRectMake((self.view.bounds.size.width - KBtnSize), (self.view.bounds.size.height - KBtnSize * KButtonSecond), KBtnSize, KBtnSize)]; 74     // scratch 75     [self createBtn:@"scratch" andCGRect:CGRectMake((self.view.bounds.size.width - KBtnSize), (self.view.bounds.size.height - KBtnSize * KButtonThird), KBtnSize, KBtnSize)]; 76      77     // 猫身上的按钮 78     // 头 79     [self createBtn:@"knockout" andCGRect:CGRectMake(62, 80, 190, 162)]; 80     // 尾巴 81     [self createBtn:@"angry" andCGRect:CGRectMake(212, 352, 35, 80)]; 82     // 左脚 83     [self createBtn:@"footLeft" andCGRect:CGRectMake(158, 430, 45, 32)]; 84     // 右脚 85     [self createBtn:@"footRight" andCGRect:CGRectMake(112, 430, 45, 32)]; 86      87      88 } 89  90 /** 创建点击按钮 */ 91 - (void) createBtn:(NSString *)btnTitle andCGRect:(CGRect)frame 92 { 93     UIButton *btn = [[UIButton alloc] initWithFrame:frame]; 94     [btn setTitle:btnTitle forState:UIControlStateNormal]; 95     [btn setTitleColor:[UIColor clearColor] forState:UIControlStateNormal]; 96      97     // 设置按钮背景图片 98     UIImage *imgs = [UIImage imageNamed:btnTitle]; 99     if (imgs) {100             [btn setBackgroundImage:imgs forState:UIControlStateNormal];101     } else {102     }103 104     105     // 监听点击事件106     [btn addTarget:self action:@selector(playAnimation:) forControlEvents:UIControlEventTouchUpInside];107     108     [self.view addSubview:btn];109 }110 111 /** 点击事件 */112 - (void)playAnimation : (UIButton *)button113 {114     if( self.tom.isAnimating) return;115     // 获取点击按钮的名称116     NSString *str = [button titleForState:UIControlStateNormal];117     118     // 根据点击按钮的名称从plist文件中获取图片数量119     int count = [self.dict[str] intValue];120     121     // 创建图片数组122     NSMutableArray *imageArray = [NSMutableArray array];123     124     // 循环获取所以图片125     for (int i = 0; i < count; i++) {126         // 图片名称127         NSString *filename = [NSString stringWithFormat:@"%@_%02d.jpg", str, i];128         // 图片绝对路径129         NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:nil];130         // 获取图片131         UIImage *img = [[UIImage alloc] initWithContentsOfFile:path];132         // 将图片存入图片数组133         [imageArray addObject:img];134         135     }136     137     // 创建图片动画138     self.tom.animationImages = imageArray;139     // 每0.075秒播一张图片140     self.tom.animationDuration = count * 0.075;141     // 只播一次142     self.tom.animationRepeatCount = 1;143     // 开始动画144     [self.tom startAnimating];145     // 动画结束后销毁图片数组146     [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration];147 }148 149 150 - (void)didReceiveMemoryWarning151 {152     [super didReceiveMemoryWarning];153     // Dispose of any resources that can be recreated.154 }155 156 @end

 

转载于:https://www.cnblogs.com/wentianblog/p/3742614.html

你可能感兴趣的文章
苹果手表:大方向和谷歌一样,硬件分道扬镳
查看>>
ccf 出现次数最多的数
查看>>
单例模式
查看>>
Competing Consumers Pattern (竞争消费者模式)
查看>>
HDUOJ ------1398
查看>>
cf--------(div1)1A. Theatre Square
查看>>
Android面试收集录15 Android Bitmap压缩策略
查看>>
Tomcat 报错的解决方法:The APR based Apache Tomcat Native library which allows optimal
查看>>
最长公共子串问题(LCS)
查看>>
TortoiseSVN is locked in another working copy
查看>>
PHP魔术方法之__call与__callStatic方法
查看>>
ubuntu 安装后的配置
查看>>
Html学习_简易个人网页制作
查看>>
angular中ng-bind指令小案例
查看>>
jqery总结
查看>>
Lodop获取客户端主网卡ip地址是0.0.0.0
查看>>
VSCODE更改文件时,提示:EACCES: permission denied的解决办法(mac电脑系统)
查看>>
web前端之路,js的一些好书(摘自聂微东 )
查看>>
【模板】对拍程序
查看>>
微信小程序开发初体验
查看>>