AutoLayout-代码布局添加动画
2015-05-23 00:00
423 查看
摘要: AutoLayout动画
开始之前先推荐老外封装好的开源库Masonry,Masonry是一个轻量级的布局框架,拥有自己的描述语法,简洁明了并具有高可读性。下面得例子用系统API实现一个和Masonry一样得布局,点击一个Button改变尺寸,Button尺寸不能超过VC界面;
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/19/3e39d3cdb33ec41fb02728fe78213e2d.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/19/cb0c078c0662cd6070677e64a7eb41d9.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/19/2acef992c1a6ec7d40950027bb6d78b4.png)
添加一个button在VC中心
添加button
添加宽度约束
添加高度约束
添加中心点约束
添加button的最大宽度和高度,这里要注意的是,对同一个属性添加多个约束要设置优先级,否则会报错。
button点击事件
附源代码 https://github.com/p-p-b/AutoLayoutDemo
开始之前先推荐老外封装好的开源库Masonry,Masonry是一个轻量级的布局框架,拥有自己的描述语法,简洁明了并具有高可读性。下面得例子用系统API实现一个和Masonry一样得布局,点击一个Button改变尺寸,Button尺寸不能超过VC界面;
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/19/3e39d3cdb33ec41fb02728fe78213e2d.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/19/cb0c078c0662cd6070677e64a7eb41d9.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/19/2acef992c1a6ec7d40950027bb6d78b4.png)
添加一个button在VC中心
@interface PPBUpdateVC() @property (nonatomic,strong) UIButton *growingButton; @property (nonatomic,strong) NSLayoutConstraint *layoutWith; @property (nonatomic,strong) NSLayoutConstraint *layoutHeight; @end
添加button
self.growingButton = [UIButton buttonWithType:UIButtonTypeSystem]; [self.growingButton setTitle:@"Grow Me!" forState:UIControlStateNormal]; self.growingButton.layer.borderColor = UIColor.greenColor.CGColor; self.growingButton.layer.borderWidth = 3; self.growingButton.translatesAutoresizingMaskIntoConstraints = NO; [self.growingButton addTarget:self action:@selector(didTapGrowButton:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.growingButton];
添加宽度约束
self.layoutWith = [NSLayoutConstraint constraintWithItem:self.growingButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100]; self.layoutWith.priority = UILayoutPriorityDefaultHigh; [self.growingButton addConstraint:self.layoutWith];
添加高度约束
self.layoutHeight = [NSLayoutConstraint constraintWithItem:self.growingButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100]; self.layoutHeight.priority = UILayoutPriorityDefaultHigh; [self.growingButton addConstraint:self.layoutHeight];
添加中心点约束
NSLayoutConstraint *centerX = [NSLayoutConstraint constraintWithItem:self.growingButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]; NSLayoutConstraint *centerY = [NSLayoutConstraint constraintWithItem:self.growingButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]; [self.view addConstraint:centerX]; [self.view addConstraint:centerY];
添加button的最大宽度和高度,这里要注意的是,对同一个属性添加多个约束要设置优先级,否则会报错。
self.layoutWith.priority = UILayoutPriorityDefaultHigh;
NSLayoutConstraint *maxWidth = [NSLayoutConstraint constraintWithItem:self.growingButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]; [self.view addConstraint:maxWidth]; [self.view addConstraint: [NSLayoutConstraint constraintWithItem:self.growingButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationLessThanOrEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0]]; }
button点击事件
- (void)didTapGrowButton:(UIButton *)button { self.layoutHeight.constant = self.layoutHeight.constant * 1.3; self.layoutWith.constant = self.layoutWith.constant * 1.3; [UIView animateWithDuration:0.4 animations:^{ [self.growingButton layoutIfNeeded]; }]; }
附源代码 https://github.com/p-p-b/AutoLayoutDemo
相关文章推荐
- 【iOS开发-113】在storyboard上用AutoLayout,纯代码实现AutoLayout布局方法以及简单动画
- 【iOS开发-113】在storyboard上用AutoLayout,纯代码实现AutoLayout布局方法以及简单动画
- Android布局中LayoutInflater的使用(利用代码添加xml形式的Layout布局)
- Android开发UI之补间动画-布局添加动画
- IOS NSLayoutConstraint 页面布局(通过代码添加约束)
- Silverlight开发历程—C#代码添加动画
- 代码动态添加动画效果
- 纯代码自动布局添加约束学习(-)
- AutoLayout自动布局添加约束的规则
- Android-UI布局---RecyclerView学习(六)item添加删除展现的动画效果
- Button 在布局文件中定义监听器,文字阴影,自定义图片,代码绘制样式,添加音效的方法
- Android动画效果之自定义ViewGroup添加布局动画
- UIView基础动画(推荐多使用Block)---能实现很多效果,透明--不透明 缩放--从无到整 Block之间添加代码也就是开始动画与结束动画之间
- 布局添加动画效果
- 属性动画代码及布局
- AndroidUI 布局动画-为列表添加布局动画效果
- iOS第三方库Eureka实现定制动画详解(三):Eureka的代码布局
- unity代码添加动画,并传参数
- Android 在布局中用代码随意添加视图