16-UIKit(AutoLayout、Animation)
目录:
一、AutoLayout自动布局
1.什么是AutoLayout
从ios6开始引入的新技术,是新版的自动布局技术
2.基本原理
通过对视图中所有子视图添加各种约束的方式实现布局,约束是一种规则,保存在父视图中,约束(constraint)类似于:
此子视图相对父视图的左边一定20个点
此子视图相对父视图的上边一定20个点
此子视图宽100个点,高30个点。
3.使用AutoLayout
[MX1-AutoLayout]按住ctrl向上下左右托
4.约束的颜色:
4.1设置约束两大原则
1>描述清晰
2>互不冲突
4.2颜色的含义
橘黄色:描述不清楚
蓝色:满足两大原则
红色:冲突
5.各种约束
1>对齐的约束
2>间距的约束(双击线条可以修改间距)
3>大小的约束(宽高)
4>最佳大小
一些常用控件(或视图),拥有最佳大小属性,所以即使不添加宽高的约束,也满足两大原则,如UIButton,UILabel,UIStepper,但如果控件没有最佳大小属性,那么在自动布局中就需要设置宽高(托自己),否则无法满足两大原则中的描述清晰原则,如UIImageView
5>参考其他控件(托到其他控件)
练习:[MX2]
[MX3]两个按钮在屏幕的上方,左按钮离屏幕左10个点,右按钮离屏幕右10个点,两个按钮间10个点,两个按钮等宽,水平对齐,离状态栏10个点
developer.apple.com
wwdc2013 session 406 AutoLayout
6.用代码方法实现自动布局
[MX4-AutoLayout-Code]
6.1万能公式
view1.attr1 = view2.attr2 * multiplier + constant
如:button.top = self.view.top * 1 + 10
- (void)viewDidLoad
{ [super viewDidLoad]; // button布局约束 UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [button setBackgroundColor:[UIColor greenColor]]; CGRect frame = CGRectMake(, , , ); button.frame = frame; // 1.先加入到父视图 [self.view addSubview:button]; // AutoLayout代码应该写在此处,2.在设置约束 // view1.attr1 = view2.attr2 * multiplier + constant // button.top = self.view.top * 1 + 10 NSLayoutConstraint *topMargin = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier: constant:]; // button.right = self.view.top * 1 - 10 NSLayoutConstraint *rightMargin = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier: constant:-]; // button.width = nil + 100; NSLayoutConstraint *widthMargin = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier: constant:]; // button.height = nil = 50; NSLayoutConstraint *heithtMargin = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier: constant:]; NSArray *constraints = @[topMargin,rightMargin,widthMargin,heithtMargin]; [self.view addConstraints:constraints]; // 解决AutoLayout和AutoResizing之间的冲突,将Autoresizing自动翻译成AutoLayout约束的动作取消 self.view.translatesAutoresizingMaskIntoConstraints = NO; button.translatesAutoresizingMaskIntoConstraints = NO; } -(void)viewDidLayoutSubviews{ // 这儿的代码是纯代码布局时使用,只用来改变子视图的frame // 不能在此写AutoLayout布局代码 }
6.2VFL(Visual Format Language)
利用以下方法创建多个约束
+ (NSArray *)constraintsWithVisualFormat:(NSString *)format options:(NSLayoutFormatOptions)opts metrics:(NSDictionary *)metrics views:(NSDictionary *)views;
格式化字符串:
|-20-[button1]-10-[button2(==button1)]-10-[button3(==button1)]-20-| 水平方向
V:|-20-[button1] 垂直方向
[MX5-VFL]
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
[button1 setTitle:@"button1" forState:UIControlStateNormal];
button1.backgroundColor = [UIColor grayColor];
[self.view addSubview:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
[button2 setTitle:@"button2" forState:UIControlStateNormal];
button2.backgroundColor = [UIColor grayColor];
[self.view addSubview:button2];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];
[button3 setTitle:@"button3" forState:UIControlStateNormal];
button3.backgroundColor = [UIColor grayColor];
[self.view addSubview:button3];
// 关掉Autoresizing
button1.translatesAutoresizingMaskIntoConstraints = NO;
button2.translatesAutoresizingMaskIntoConstraints = NO;
button3.translatesAutoresizingMaskIntoConstraints = NO;
// 创建多个约束
NSString *formatString = @"|-10-[btn1]-10-[btn2(==btn1)]-10-[btn3(==btn1)]-10-|";// 水平方向
NSDictionary *views = @{@"btn1" : button1, @"btn2" : button2, @"btn3" : button3};
NSArray *constranints = [NSLayoutConstraint constraintsWithVisualFormat:formatString options:NSLayoutFormatAlignAllCenterY metrics:nil views:views];
[self.view addConstraints:constranints];
formatString = @"V:|-20-[btn1]";// 垂直方向
constranints = [NSLayoutConstraint constraintsWithVisualFormat:formatString options:NSLayoutFormatAlignAllCenterY metrics:nil views:views];
[self.view addConstraints:constranints];
}
7.自动布局总结
1>代码自动布局尽量避免
2>使用自动布局时,尽量在程序中不要直接改变被自动布局的视图的frame
3>当控件或子视图非常多时,自动布局会发挥更大的作用
4>如果使用transform,就无法使用自动布局
二、动画(Animation)
1.概念:把一些静态的图片快速切换就会形成动画,每一张静态的图片叫一帧,如果25~30帧/秒,人眼就无法识别帧间切换,感觉起来就是动画了。
cocos2D-X 1/60 FPS帧率(一帧需要多少秒来播放)
2.ios动画
可以用多种方式实现,一种最简单的方式:
UIImage类,animatedImageNamed:duration:
一般用做非常简单,图片比较小,动画过程比较固定的动画
[MX6-Animation-UIImage]
- (void)viewDidLoad
{
[super viewDidLoad]; self.imageView.image = [UIImage animatedImageNamed:@"ship-anim" duration:1.0/ * ];
}
3.NSTimer定时器
在指定的时间内向指定对象发送指定的方法,可以设置重复
[mx7-NSTimer]
- (IBAction)connect:(UIBarButtonItem *)sender {
[NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(titleChange:) userInfo:nil repeats:YES];
}
-(void)titleChange:(id)sender{
if (self.count == ) {
self.title = @"连接中.";
}else if(self.count == ){
self.title = @"连接中..";
}else if(self.count == ){
self.title = @"连接中...";
}else if(self.count == ){
self.title = @"连接中....";
}else if(self.count == ){
self.title = @"连接中.....";
}else if(self.count == ){
self.title = @"连接中......";
}
self.count++;
self.count %= ;
}
4.NSTimer制作动画
4.1匀速动画
在指定时间点向控制对象发消息,从而改变对象的 具体属性.frame .bounds .center .transform .alpha
[MX8- Animation-Timer]
匀速动画
当前值 = 开始值 + 当前帧数 * (目标值 - 开始值) / (帧率 * 动画时长)
- (IBAction)fadeIn:(UIButton *)sender {
[NSTimer scheduledTimerWithTimeInterval:1.0 / FPS target:self selector:@selector(fadeInAnimate:) userInfo:nil repeats:YES];
}
-(void)fadeInAnimate:(NSTimer *)sender{
// 结果值 = 开始值 + 当前帧序数 * (结束值 - 开始值)/(帧率 * 动画时长)
self.times++;
self.imageView.alpha = + self.times * (1.0 - )/(FPS * );
if (self.times >= FPS * ) {
[sender invalidate];
self.times = ;
}
NSLog(@"%d",self.times);
}
- (IBAction)fadeOut:(UIButton *)sender {
[NSTimer scheduledTimerWithTimeInterval:1.0 / FPS target:self selector:@selector(fadeOutAnimate:) userInfo:nil repeats:YES];
}
-(void)fadeOutAnimate:(NSTimer *)sender{
self.times--;
self.imageView.alpha = 1.0 - self.times * ( - 1.0)/(FPS * );
NSLog(@"%d",self.times);
if (self.times <= -FPS * ) {
[sender invalidate];
self.times = ;
}
}
4.2渐变动画
变速动画
当前值 = 上一次的值 + (目标值 - 上次值) * 渐近因子
- (void)viewDidLoad
{
[super viewDidLoad];
self.startY = self.playView.center.y;
}
- (IBAction)start:(UIButton *)sender {
[NSTimer scheduledTimerWithTimeInterval:/FPS target:self selector:@selector(animate:) userInfo:nil repeats:YES]; } -(void)animate:(NSTimer *)sender{
// 匀速动画
// 当前值 = 开始值 + 当前帧数 * (结束值 - 开始值) / (帧率 * 动画时长)
self.count++;
CGPoint center = self.playView.center;
center.y = self.startY + self.count * ( - self.startY) * 0.01;
self.playView.center = center;
if (self.playView.center.y <= ) {
[sender invalidate];
} } - (IBAction)start2:(id)sender {
[NSTimer scheduledTimerWithTimeInterval:/FPS target:self selector:@selector(animate2:) userInfo:Nil repeats:YES];
}
-(void)animate2:(NSTimer *)sender{
// 变速动画
// 当前值 = 上次值 + (结束值 - 上次值) * 渐进因子
CGPoint center = self.playView.center;
center.y = center.y + ( - center.y) * 0.1;
self.playView.center = center;
if (self.playView.center.y <= ) {
[sender invalidate];
}
}
16-UIKit(AutoLayout、Animation)的更多相关文章
- Java——搭建自己的RESTful API服务器(SpringBoot、Groovy)
这又是一篇JavaWeb相关的博客,内容涉及: SpringBoot:微框架,提供快速构建服务的功能 SpringMVC:Struts的替代者 MyBatis:数据库操作库 Groovy:能与Java ...
- 联合体union和大小端(big-endian、little-endian)
1.联合体union的基本特性——和struct的同与不同 union,中文名“联合体.共用体”,在某种程度上类似结构体struct的一种数据结构,共用体(union)和结构体(struct)同样可以 ...
- C# 计算字符串/文件的哈希值(MD5、SHA)
原文 C# 计算字符串的哈希值(MD5.SHA) 已做修改 一.关于本文 本文中是一个类库,包括下面几个函数: /// 1)计算32位MD5码(大小写):Hash_MD5_32 /// 2)计算16位 ...
- MVC—WebAPI(调用、授权)
ASP.NET MVC—WebAPI(调用.授权) 本系列目录:ASP.NET MVC4入门到精通系列目录汇总 微软有了Webservice和WCF,为什么还要有WebAPI? 用过WCF的人应该 ...
- 类图class的关联关系(聚合、组合)
类图class的关联关系(聚合.组合) 关联的概念 关联用来表示两个或多个类的对象之间的结构关系,它在代码中表现为一个类以属性的形式包含对另一个类的一个或多个对象的应用. 程序演示:关联关系(code ...
- Python/MySQL(一、基础)
Python/MySQL(一.基础) mysql: MYSQL : 是用于管理文件的一个软件 -socket服务端 (先启动) -本地文件操作 -解析 指令[SQL语句] -客户端软件 (各种各样的客 ...
- 09_Java面向对象_第9天(类、封装)_讲义
今日内容介绍 1.面向对象思想 2.类与对象的关系 3.局部变量和成员变量的关系 4.封装思想 5.private,this关键字 6.随机点名器 01面向对象和面向过程的思想 A: 面向过程与面向对 ...
- [转]SQL Server 2012 的 T-SQL 新功能 – 新的数据分析函数(LEAD、LAG)
当您需要在 SQL Server 中利用 T-SQL 比较结果集的每一列跟前一列或后一列的差异时,在过去可能需要利用 CURSOR 搭配临时表变量,或是透过递归 CTE 来达到这个效果,如今 SQL ...
- 查看执行计划plustrace:set autotrace trace exp stat(SP2-0618、SP2-0611)
执行计划是SQL获取和处理数据的途径和方法. 执行计划和性能 SQL -- 数据库性能的始作俑者 所有的数据库性能,几乎全部来自SQL. 优秀的SQL是数据库最大的福祉. 一条很烂的SQL,可以搞瘫一 ...
随机推荐
- Object lifetime
Object lifetime Temporary object lifetime Storage reuse Access outside of lifetime Every object has ...
- Android目录结构介绍&Android学习之hello world
分类: 嵌入式 一个android项目有如下目录: src:这里放的是我们编写的源代码 gen:这里的是eclipse自动生成的文件,不用管它 asssts:放置文件 res:也是放置文件,不同的是r ...
- mysql 表及其列字符集设置
--修改表的字符集 alter table rtb_media_daily_report character set gbk; --查询表列字符集 show full columns from rtb ...
- [mysql]子查询与连接
1,子查询(Subquery)是指出现在其他 SQL 语句内的select子句 例如: select * from t1 where col1 = (select col2 from t2); 其中 ...
- PROTEL99 SE生成的gerber 与ncdrill的坐标不对应
导入cam350后的: 解决方法:出gerber的时候在高级选项里面. 1.数据单位及格式 2.优化设置
- QT直接支持GB18030,附它对此编码的一堆相关文档描述
http://doc.qt.io/qt-5/qtextcodec.html http://doc.qt.io/qt-5/codec-gbk.html
- Poj 1002 487-3279(二叉搜索树)
题目链接:http://poj.org/problem?id=1002 思路分析:先对输入字符进行处理,转换为标准形式:插入标准形式的电话号码到查找树中,若有相同号码计数器增加1,再中序遍历查找树. ...
- Hive实现oracle的Minus函数
在Oracle中minus运算的主要功能是: 在进行两个表格或者两个查询结果的时候,返回在第一个表格/查询结果中与第二个表格/查询结果不同样的记录. 结果不同样的记录包括两种情况:A,B 表中某一行的 ...
- NYIST 914Yougth的最大化【二分搜索/Dinkelbach算法】
转载请注明出处:http://www.cnblogs.com/KirisameMarisa/p/4187637.html 题目链接:http://acm.nyist.net/JudgeOnline/p ...
- 永久性for循环配合switch语句可以实现菜单功能
永久性for循环配合switch语句可以实现菜单功能总结:加入想要无条件地跳转到某条语句执行,用goto语句: 加入想要对某种条件进行判断,为真或为假分别执行不同的语句,用if语句 加入想要检测的条件 ...