(9/18)重学Standford_iOS7开发_动画、自动布局_课程笔记
最近开始实习,没多少时间更新了=_=
第九课:
1、上节课demo:Dropit完整实现
https://github.com/NSLogMeng/Stanford_iOS7_Study/commit/cfc24c2c4300185d972cf151872d313414a48d32
2、Autolayout
比较简单请参照课件截图https://github.com/NSLogMeng/Stanford_iOS7_Study/blob/master/Slides/Lecture%209%20Slides.pdf
课程实现方法为在storyboard中可视化实现,本文再介绍两种方法:
①NSLayoutConstraint
+(instancetype)constraintWithItem:(id)view1
attribute:(NSLayoutAttribute)attr1
relatedBy:(NSLayoutRelation)
relation toItem:(id)view2
attribute:(NSLayoutAttribute)attr2
multiplier:(CGFloat)multiplier constant:(CGFloat)c;
//约束:item1.attribute =(>=,<=related) multiplier * item2.attribute + constant //使用UIView添加约束
- (void)addConstraint:(NSLayoutConstraint *)constraint
②Visual Format Language(VFL)
官方文档很简单易懂
语法:
| 含义 | 语法 | 含义 | 语法 |
| 水平方向 | H: | 关系 | >=,<=,== |
| 垂直方向 | V: | 空间,间隙 | - |
| Views | [view] | 优先级 | @value |
| superView | | |
举例:









代码实现举例:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *button1 = [[UIButton alloc] init];
button1.backgroundColor = [self randomColor];
[button1 setTitle:@"button1" forState:UIControlStateNormal];
button1.translatesAutoresizingMaskIntoConstraints = NO;//手动创建的代码使用VFL必须更改此属性
button1.layer.cornerRadius = 4.0f;
[self.view addSubview:button1];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[button1]-|" options: metrics:nil views:NSDictionaryOfVariableBindings(button1)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[button1(width)]" options: metrics:@{@"width" : @} views:NSDictionaryOfVariableBindings(button1)]];
UIButton *button2 = [[UIButton alloc] init];
button2.backgroundColor = [self randomColor];
[button2 setTitle:@"button2" forState:UIControlStateNormal];
button2.translatesAutoresizingMaskIntoConstraints = NO;//手动创建的代码使用VFL必须更改此属性
button2.layer.cornerRadius = 4.0f;
[self.view addSubview:button2];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[button2]-|" options: metrics:nil views:NSDictionaryOfVariableBindings(button2)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[button1]-44-[button2(30)]" options: metrics:nil views:NSDictionaryOfVariableBindings(button1,button2)]];
UITabBar *tabBar = [[UITabBar alloc] init];
tabBar.barTintColor = [UIColor colorWithRed:/255.0f green:/255.0f blue:/255.0f alpha:1.0f];
tabBar.translatesAutoresizingMaskIntoConstraints = NO;//手动创建的代码使用VFL必须更改此属性
UITabBarItem *item1 = [[UITabBarItem alloc] init];
[item1 setTitle:@"item1"];
UITabBarItem *item2 = [[UITabBarItem alloc] init];
[item2 setTitle:@"item2"];
UITabBarItem *item3 = [[UITabBarItem alloc] init];
[item3 setTitle:@"item3"];
tabBar.items = @[item1,item2,item3];
[self.view addSubview:tabBar];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tabBar]|" options: metrics:nil views:NSDictionaryOfVariableBindings(tabBar)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[tabBar(45)]|" options: metrics:nil views:NSDictionaryOfVariableBindings(tabBar)]];
[button1 addTarget:self action:@selector(touchButton1) forControlEvents:UIControlEventTouchDown];
}
注意:①代码创建的view要加 button1.translatesAutoresizingMaskIntoConstraints = NO;才能生效
②metrics存储VFL中可变量的值(字典),可以参考button1的约束
③views参数对应VFL中View的值(通过NSDictionaryOfVariableBindings(...)实现)
3、作业
还没做=_=,后面补。
课程视频地址:网易公开课:http://open.163.com/movie/2014/1/B/P/M9H7S9F1H_M9H80K2BP.html
或者iTunes U搜索standford课程
(9/18)重学Standford_iOS7开发_动画、自动布局_课程笔记的更多相关文章
- (1/18)重学Standford_iOS7开发_iOS概述_课程笔记
写在前面:上次学习课程对iOS还是一知半解,由于缺乏实践,看公开课的视频有时不能很好地领会知识.带着问题去学习永远是最好的方法,接触一段时间iOS开发以后再来看斯坦福iOS公开课,又会有许多新的发现, ...
- (3/18)重学Standford_iOS7开发_Objective-C_课程笔记
第三课: 本节课主要是游戏实现的demo,因此我将把课程中简单的几个编程技巧提取出来,重点介绍如何自己实现作业中的要求. 纸牌游戏实现: ①游戏的进行是模型的一部分(理解什么是模型:Model = W ...
- (8/18)重学Standford_iOS7开发_协议、block、动画_课程笔记
第八课: 1.协议 另一种安全处理id类型的方式如:id <MyProtocol> obj a.声明 //协议一般放于.h文件中或者在类的.h文件中 @protocol Foo <X ...
- (6/18)重学Standford_iOS7开发_控制器多态性、导航控制器、选项卡栏控制器_课程笔记
终于有时间跟新了,两周时间复(yu)习(xi)了5门考试累觉不爱...... ------------------------------------------------------------- ...
- (7/18)重学Standford_iOS7开发_视图、绘制、手势识别_课程笔记
第七课: 1.View 一般来说,视图是一个构造块,代表屏幕上一块矩形区域,定义了一个坐标空间,并在其中绘制及添加触控事件等. ①视图的层级关系 一个视图只能有一个父视图,可以有多个子视图 - ( - ...
- (4/18)重学Standford_iOS7开发_框架和带属性字符串_课程笔记
第四课(干货课): (最近要复习考试,有点略跟不上节奏,这节课的内容还是比较重要的,仔细理解掌握对今后的编程会有很大影响) 本节课主要涉及到Foundation和UIKit框架,基本都是概念与API知 ...
- (5/18)重学Standford_iOS7开发_视图控制器生命周期_课程笔记
第五课: 1.UITextView @property (nonatomic, readonly) NSTextStorage *textStorage;//注意为只读属性,因此不能直接更改内容,NS ...
- (2/18)重学Standford_iOS7开发_Xcode_课程笔记
第二课: 1.惰性初始化 -(ObjectType *)example { f(!_example) example =[[ObjectType alloc] init]; return _examp ...
- 重学 Java 设计模式:实战外观模式「基于SpringBoot开发门面模式中间件,统一控制接口白名单场景」
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 你感受到的容易,一定有人为你承担不容易 这句话更像是描述生活的,许许多多的磕磕绊绊总 ...
随机推荐
- Mysql锁机制和事务控制
如何加锁 锁定表的语法: LOCK TABLES tbl_name [AS alias] {READ [LOCAL] | [LOW_PRIORITY] WRITE} [, tbl_n ...
- 以中断方式实现1s定时
中断方式比较特殊,需要使用单片机内部的中断处理机制,同时指定中断函数. #include <reg52.h> sbit LED = P0^; unsigned ; void main() ...
- 使用WMI监控进程启动与结束
需要添加引用System.Management 代码: static void Main(string[] args) { //创建WQL事件查询,监视进程开启 var qCreate = new W ...
- KVC - 键值编码
[基本概念] 1.键值编码是一个用于间接访问对象属性的机制,使用该机制不需要调用存取方法和变量实例就可访问对象属性. 2.键值编码方法在OC非正式协议(类目)NSKeyValueCoding中被声明, ...
- smarty 比较运算符对照表
smarty 比较运算符对照表 运算符 中文解释 eq 相等 ne.neq 不相等 gt 大于 lt 小于 gte.ge 大于等于 lte.le 小于等于 not 非 mod 求模 is [not] ...
- Ecmall系统自带的分页功能
在Ecmall的二次开发中,分页是必不可少的.这个系统已经自带了分页功能,下面来看看如何使用这个分页. 下面是一个自定义的类,用于查看订单的详细情况.关键在于get_order_data()这个方法, ...
- 【BZOJ 2820】 YY的GCD (莫比乌斯+分块)
YY的GCD Description 神犇YY虐完数论后给傻×kAc出了一题 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少 ...
- 怎么清除SVN密码,以及重置eclipse中svn插件密码
如何清除SVN密码,以及重置eclipse中svn插件密码? 清除SVN客户端密码方法: 邮件选择TortoiseSVN中的settings选项---Saved Data---右边会发现有个Authe ...
- Android MediaPlayer状态机
对播放音频/视频文件和流的控制是通过一个状态机来管理的.下图显示一个MediaPlayer对象被支持的播放控制操作驱动的生命周期和状态.椭圆代表MediaPlayer对象可能驻留的状态.弧线表示驱动M ...
- android Service开机启动及debug
开机启动一个service需要做的工作如下: 1.开发一个receiver用于接收系统广播: public class BootReceiver extends BroadcastReceiver { ...