UI基础第二天(代码)
来源:互联网 发布:里诺仓库管理软件sql 编辑:程序博客网 时间:2024/05/21 17:02
项目:汤姆猫动画
方法:
- 编写创建界面的方法(按钮和图片视图)
- 编写出初始化界面的方法
- 编写动画的播放动画的方法
- 编写按钮响应事件的方法
素材:
- 加载显示的图片放在Images.xcassets里
- 动画播放的图片放在根目录下
代码实现:
#import "ViewController.h"@interface ViewController ()//播放的视图@property (weak, nonatomic) UIImageView *imageView;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; //创建界面并初始化界面 [self buildInterface];}//创建界面并初始化界面-(void)buildInterface{ //创建视图 self.imageView = [self buildImageView]; self.imageView.image = [UIImage imageNamed:@"cat"]; //创建按钮 UIButton* button0 = [self buildButtonWithFrame:(CGRect){16,587,60,60} andTag:0]; UIButton* button1 = [self buildButtonWithFrame:(CGRect){16,516,60,60} andTag:1]; UIButton* button2 = [self buildButtonWithFrame:(CGRect){16,443,60,60} andTag:2]; UIButton* button3 = [self buildButtonWithFrame:(CGRect){299,443,60,60} andTag:3]; UIButton* button4 = [self buildButtonWithFrame:(CGRect){299,516,60,60} andTag:4]; UIButton* button5 = [self buildButtonWithFrame:(CGRect){299,587,60,60} andTag:5]; [self buildButtonWithFrame:(CGRect){91,119,192,199} andTag:6]; [self buildButtonWithFrame:(CGRect){130,363,112,226} andTag:7]; [self buildButtonWithFrame:(CGRect){196,602,46,34} andTag:8]; [self buildButtonWithFrame:(CGRect){130,607,46,34} andTag:9]; //设置按钮背景图片 [button0 setBackgroundImage:[UIImage imageNamed:@"b0"]forState:UIControlStateNormal]; [button1 setBackgroundImage:[UIImage imageNamed:@"b1"]forState:UIControlStateNormal]; [button2 setBackgroundImage:[UIImage imageNamed:@"b2"]forState:UIControlStateNormal]; [button3 setBackgroundImage:[UIImage imageNamed:@"b3"]forState:UIControlStateNormal]; [button4 setBackgroundImage:[UIImage imageNamed:@"b4"]forState:UIControlStateNormal]; [button5 setBackgroundImage:[UIImage imageNamed:@"b5"]forState:UIControlStateNormal];}//创建视图-(UIImageView*) buildImageView{ UIImageView * imageView =[[UIImageView alloc]init]; //加入父视图 [self.view addSubview:imageView]; //设置尺寸 imageView.frame = (CGRect){0,0,375,667}; return imageView;}//创建按钮-(UIButton*) buildButtonWithFrame:(CGRect)frame andTag:(int)tag{ UIButton * button = [[UIButton alloc]init]; //加入父视图 [self.view addSubview:button]; //设置属性 button.frame = frame; button.tag = tag; //添加响应方法 [button addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside]; return button;}//执行动画-(void) start:(UIButton*)button{ switch (button.tag) { case 0: [self animationWithImageCount:81 andImageName:@"drink"]; break; case 1: [self animationWithImageCount:40 andImageName:@"eat"]; break; case 2: [self animationWithImageCount:24 andImageName:@"pie"]; break; case 3: [self animationWithImageCount:28 andImageName:@"fart"]; break; case 4: [self animationWithImageCount:13 andImageName:@"cymbal"]; break; case 5: [self animationWithImageCount:56 andImageName:@"scratch"]; break; case 6: [self animationWithImageCount:81 andImageName:@"knockout"]; break; case 7: [self animationWithImageCount:26 andImageName:@"angry"]; break; case 8: [self animationWithImageCount:30 andImageName:@"footLeft"]; break; case 9: [self animationWithImageCount:30 andImageName:@"footRight"]; break; }}//加载并播放动画-(void) animationWithImageCount:(int)count andImageName:(NSString*)name{ //如果当前正在执行动画就直接退出 if (self.imageView.isAnimating) { return; } //创建可变数组,用于存放动画的图片 NSMutableArray* imageArray = [NSMutableArray arrayWithCapacity:count]; //加载图片 for (int i=0; i<count; i++) { //图片的名称 NSString* imageName = [NSString stringWithFormat:@"%@_%02d",name,i]; //根据图片名称创建图片,使用 imageNamed: 创建出来的图片默认会进行缓存 //UIImage* image = [UIImage imageNamed:imageName]; //NSBundle:应用所在文件夹, NSBundle* bundele =[NSBundle mainBundle]; //获取图片全路径,在images.xcassets中图片会被压缩到一个包中,无法获取它的全路径 NSString* path = [bundele pathForResource:imageName ofType:@"jpg"]; //根据路径创建图片 UIImage* image = [UIImage imageWithContentsOfFile:path]; //把图片添加到数组中 [imageArray addObject:image]; } //把图片传递给视图 self.imageView.animationImages = imageArray; //播放的时间 self.imageView.animationDuration = count * 0.05; //播放的次数 self.imageView.animationRepeatCount = 1; //开始播放 [self.imageView startAnimating]; //延迟清空动画 [self.imageView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.imageView.animationDuration+0.05]; }@end
0 0
- UI基础第二天(代码)
- UI基础第二天(知识点)
- UI基础第二天(自学)
- UI-第二讲:基础控件
- UI基础第一天(代码)
- UI基础第三天(代码)
- [UI基础]day01(代码)
- Android学习笔记---第二天---基础UI组件---Toast
- Android学习笔记---第二天---基础UI组件---TextView
- Android学习笔记---第二天---基础UI组件---EditText
- UI基础第二弹:Button基础
- 《每天3分钟学开发》ios开发之UI基础:UI 第二天!
- UI第二天总结
- UI第二天
- Python(基础)第二天
- Android UI基础java代码基础点
- JAVA基础第二天;基本代码的编写(15-6-17)
- UI第二天:第⼆讲:基础视图、程序启动流程
- 极角排序 POJ1696
- 好的架构师,挖空心思偷懒
- 气泡样式提示框
- iOS开发之自定义表情键盘(组件封装与自动布局)
- Error:Failed to create directory 'C:\Users\Administrator\.gradle\caches\2.8\scripts\ijinit7_5jx13p26
- UI基础第二天(代码)
- js 字符串截取(包含中英文)
- 如何捕获java线程中的逃逸的异常
- sublime text
- 决策树学习笔记整理
- 用OpenCV实现图像的水平镜像(翻转)变换和竖直镜像(翻转)变换(垂直镜像变换)的源码
- 前端交互开发微体验--总结了一些国内外炫酷的网站
- 安卓studio添加debug指纹证书
- iOS开发之手势识别