前言

说到iOS自动布局,有很多的解决办法。有的人使用xib/storyboard自动布局,也有人使用frame来适配。对于前者,笔者并不喜欢,也不支持。对于后者,更是麻烦,到处计算高度、宽度等,千万大量代码的冗余,对维护和开发的效率都很低。

笔者在这里介绍纯代码自动布局的第三方库:Masonry。这个库使用率相当高,在全世界都有大量的开发者在使用,其star数量也是相当高的。

效果图

本节详解Masonry的以动画的形式更新约束的基本用法,先看看效果图:

我们这里初始按钮是一个很小的按钮,点击就不断放大,最大就放大到全屏幕。

核心代码

看下代码:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
 
@interface CompositeController ()
 
@property (nonatomic, strong) NSMutableArray *viewArray;
@property (nonatomic, assign) BOOL isNormal;
 
@end
 
@implementation CompositeController
 
- (void)viewDidLoad {
  [super viewDidLoad];
  
  UIView *lastView = self.view;
  for (int i = 0; i < 6; i++) {
    UIView *view = UIView.new;
    view.backgroundColor = [self randomColor];
    view.layer.borderColor = UIColor.blackColor.CGColor;
    view.layer.borderWidth = 2;
    [self.view addSubview:view];
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
      make.edges.equalTo(lastView).insets(UIEdgeInsetsMake(20, 20, 20, 20));
    }];
    
    lastView = view;
    
    [self.viewArray addObject:view];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                          action:@selector(onTap)];
    [view addGestureRecognizer:tap];
  }
  self.isNormal = YES;
}
 
- (void)onTap {
    UIView *lastView = self.view;
  
  if (self.isNormal) {
    for (NSInteger i = self.viewArray.count - 1; i >= 0; --i) {
      UIView *itemView = [self.viewArray objectAtIndex:i];
      [itemView mas_remakeConstraints:^(MASConstraintMaker *make) {
       make.edges.equalTo(lastView).insets(UIEdgeInsetsMake(20, 20, 20, 20));
      }];
      
      [self.view bringSubviewToFront:itemView];
      lastView = itemView;
    }
  } else {
    for (NSInteger i = 0; i < self.viewArray.count; ++i) {
      UIView *itemView = [self.viewArray objectAtIndex:i];
      [itemView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(lastView).insets(UIEdgeInsetsMake(20, 20, 20, 20));
      }];
      
      [self.view bringSubviewToFront:itemView];
      lastView = itemView;
    }
  }
  
  [self.view setNeedsUpdateConstraints];
  [self.view updateConstraintsIfNeeded];
  
  [UIView animateWithDuration:0.5 animations:^{
    [self.view layoutIfNeeded];
  } completion:^(BOOL finished) {
    self.isNormal = !self.isNormal;
  }];
}
 
- (NSMutableArray *)viewArray {
  if (_viewArray == nil) {
    _viewArray = [[NSMutableArray alloc] init];
  }
  
  return _viewArray;
}
 
- (UIColor *)randomColor {
  CGFloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0
  CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from white
  CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from black
  
  return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}
 
@end
 

讲解


移除之前的所有约束,然后添加新约束的方法是:mas_remakeConstraints

我们的目标是点击时,将里面的往外面,外面的往里面,并且显示动画效果。其中,最关键的代码是:

 
1
2
3
4
 
[self.view bringSubviewToFront:itemView];
lastView = itemView;
 

通过循环获取各个视图,外变成里,里变成外,我们一定要记录相对于下一个view以更新下一个视图的相对边缘。

想要更新约束时添加动画,就需要调用关键的一行代码:setNeedsUpdateConstraints,这是选择对应的视图中的约束需要更新。

对于updateConstraintsIfNeeded这个方法并不是必须的,但是有时候不调用就无法起到我们的效果。但是,官方都是这么写的,从约束的更新原理上讲,这应该写上。我们要使约束立即生效,就必须调用layoutIfNeeded此方法。看下面的方法,就是动画更新约束的核心代码:

 
1
2
3
4
5
6
7
8
9
10
 
// 告诉self.view约束需要更新
[self.view setNeedsUpdateConstraints];
// 调用此方法告诉self.view检测是否需要更新约束,若需要则更新,下面添加动画效果才起作用
[self.view updateConstraintsIfNeeded];
 
[UIView animateWithDuration:0.3 animations:^{
  [self.view layoutIfNeeded];
}];
 

Masonry自动布局:复合约束的更多相关文章

  1. Masonry自动布局

    介绍,入门: http://www.cocoachina.com/ios/20141219/10702.html 下载: http://code.cocoachina.com/detail/30114 ...

  2. Masonry控制台打印约束冲突问题解决

    不知道你是不是视图的布局也是用的第三方Masonry,在使用中是不是也遇到了控制台约束冲突的警告打印,看下图: 从输出的信息可以知道,有的控件的约束明显重复了设置,所以指出了是哪个控件,重复设置了哪些 ...

  3. Masonry自动布局使用

    Masonry是一个轻量级的布局框架,采用更好的语法封装自动布局,它有自己的布局DSL.简洁明了并具有高可读性 而且同时支持 iOS 和 Max OS X. 下载 NSLayoutConstraint ...

  4. Masonry remake更新约束

    前言 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自动布局,也有人使用frame来适配.对于前者,笔者并不喜欢,也不支持.对于后者,更是麻烦,到处计算高度.宽度等,千万 ...

  5. Masonry 动画更新约束

    前言 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自动布局,也有人使用frame来适配.对于前者,笔者并不喜欢,也不支持.对于后者,更是麻烦,到处计算高度.宽度等,千万 ...

  6. Masonry第三方代码约束

    #import "RootViewController.h" #import "Masonry.h" @interface RootViewController ...

  7. IOS7配置自动布局的约束

    上一篇博客记录了怎么使用代码对视图进行约束,原文:点击打开链接 这次记录一下关于自动布局的例子, 1.创建一个Single View Application : 2.选择自动布局: 3.拖拽两个Tex ...

  8. Masonry自动布局与UIScrolView适配

    Masonry介绍 Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布局 简洁明了 并具有高可读性 而且同时支持 iOS 和 Max OS X.可以通过cocoa ...

  9. IOS Masonry自动布局

    之前项目用Frame布局,这个项目登录用了VFL,后来觉得用Masonry,前天布局TableViewCell时用了下 ,觉得还不错. #import "Masonry.h" #i ...

随机推荐

  1. HDU-1210Eddy's 洗牌问题

    Eddy's 洗牌问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Prob ...

  2. 七牛云 X 英语流利说:教育 3.0 时代的智能突破

    美国当地时间 2018 年 9 月 27 日,国内领先的人工智能驱动的教育科技公司「英语流利说」正式挂牌纽交所,以其独创的教育 3.0 模式,成为中国「AI+ 教育」第一股. 教育 3.0 时代的智能 ...

  3. UVALive 4015 树形dp

    题目大意: 从一个根节点出发,走最多 x 的长度,问最多能走过多少个节点,图保证是一棵树 dp[0][i][j] , 表示走从i点为根的子树走过了j个点最后回到 i 最少需要多少时间dp[1][i][ ...

  4. MyEclipse 6.5安装maven插件

    一.卸载原有maven插件 MyEclipse 6.5集成了Maven插件,不过有不少bug,用习惯了m2eclipse,不想在这上面浪费时间.要安装m2eclipse,需要先把自带的maven插件卸 ...

  5. 深入理解计算机操作系统——第11章:CS模型,网络

    网络编程: 11.1 客户端-服务器编程模型 (1)一个应用是由一个服务器进程和一个或多个客户端进程组成. (2)服务器管理某种资源,并且操纵这种资源来为客户端服务. CS模型: CS的基本操作是事务 ...

  6. URAL 1614. National Project “Trams” [ 构造 欧拉回路 ]

    传送门 1614. National Project “Trams” Time limit: 0.5 secondMemory limit: 64 MB President has declared ...

  7. 洛谷——P1596 [USACO10OCT]湖计数Lake Counting

    P1596 [USACO10OCT]湖计数Lake Counting 题目描述 Due to recent rains, water has pooled in various places in F ...

  8. hdu——3861 The King’s Problem

    The King’s Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. xml解析工具mashaller javaee自带解析类

    1.怎样去掉Marshaller的格式化? : JAXBContext context = JAXBContext.newInstance(Entity.class); Marshaller mars ...

  10. Nexus搭建Maven私有仓库

    原文:http://blog.csdn.net/rickyit/article/details/54927101 前言 Nexus Repository Manager is a Javaapplic ...