pragma mark -设置界面,你可以通过第三方治本平台 Cocoapods

先前本身曾有三次越发查阅过 AutoLayout
的有关材料,可以吗其实就是一对牛牛在祥和的博客公布的利用教程及部分心得。一向都想着试一下可是一贯尚未什么日子来执行,那篇小说首要说的是自我是怎么着将
Masonry 移植到原有代码中。

如下:

假如您的品种中还尚未 Masonry,你可以经过第三方治本平台 Cocoapods
来下载,假诺有趣味的话,可以看一下自身后面整理的关于 Cocoapods
下载与安装的作品: IOS依赖管理 –
CocoaPods
.

图片 1

首先不难介绍一下 Masonry,Masonry
是一款可以让开发者非凡不难选拔iOS的机关布局(AutoLayout)机制。Masonry提供尤其健全、友好的API来替代直接运用NSLayoutConstraint举办编程,可以使视图布局的历程更为自在。(好啊我认可那句话是自己拷贝过来的,那不是生死攸关)

图片 2

而自我对 Masonry
最初的认识是经过那篇作品:Masonry介绍与运用进行:神速上手Autolayout –
 CocoaChina 苹果支付普通话站 – 最热的BlackBerry开发社区 最热的苹果开发社区
最热的平板电脑开发社区
,并且自己意识互联网上流传的多数有关
Masonry 的小说基本都来源于于此,所以倘若您对 Masonry
一点领会都尚未,那篇小说确实是很科学的。

匹配1-4条文案。所需框架Masonry

中期都铺垫完了,接下去大家就看看自己是怎么着把原先可以的代码改成了
Autolayout 的突出的代码的了

.h

这是一个饼图的控件:

@interfaceHKErrorView :UIView

图片 3

-(instancetype)initWithFrame:(CGRect)frame
withTitle1:(NSString*)title1;

事先的代码是这么的:

-(instancetype)initWithFrame:(CGRect)frame withTitle1:(NSString*)title1
withTitle2:(NSString*)title2;

因而 initWithFrame 初步化控件的时候传进来的 frame
统计饼图的半径,根据统计出来的半径确定各种 view 的安放地点:

-(instancetype)initWithFrame:(CGRect)frame withTitle1:(NSString*)title1
withTitle2:(NSString*)title2withTitle3:(NSString*)title3;

– (id)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

    if(self){

         self.backgroundColor = [UIColor clearColor];

         //UIScollView 容器

         _scrollContentView = [[UIScrollView alloc]
initWithFrame:self.bounds];

         _scrollContentView.backgroundColor = [UIColor clearColor];

         [self addSubview:_scrollContentView];

         //饼图

         _pieView = [[UIView alloc] initWithFrame:self.bounds];

         _pieView.backgroundColor = [UIColor clearColor];

         [_scrollContentView addSubview:_pieView];

         //饼图大旨白色音讯区域

         _infoView = [[UIView alloc] initWithFrame:self.bounds];

         _infoView.backgroundColor = [UIColor whiteColor];

         [_scrollContentView addSubview:_infoView];

         //颜色表明

         _descriptionView = [[UIView alloc]
initWithFrame:self.bounds];

         _descriptionView.backgroundColor = [UIColor clearColor];

         [_scrollContentView addSubview:_descriptionView];

         //统计饼图半径

         self.pieRadius = MIN(self.bounds.size.width/2,
self.bounds.size.height/2) – kMarginX*2;

         self.pieCenter = CGPointMake(self.bounds.size.width/2,
_pieRadius + kMarginY);

         _animationArr = [NSMutableArray array];

         self.textRadius = _pieRadius –
(_pieRadius-kInfoRadius)/2;

         }

    return self;

}

– (void)setPieCenter:(CGPoint)pieCenter{

    [_pieView setCenter:pieCenter];

    [_infoView setCenter:pieCenter];

    //这一个点是便于用来在饼图上边画扇叶用的,是以饼图为标准的宗旨值

    _pieCenter = CGPointMake(_pieView.frame.size.width/2,
_pieView.frame.size.height/2);

}

– (void)setPieRadius:(CGFloat)pieRadius{

    _pieRadius = pieRadius;

    CGRect frame = CGRectMake(_pieCenter.x – pieRadius, _pieCenter.y

  • pieRadius, pieRadius*2, pieRadius*2);

    _pieCenter = CGPointMake(frame.size.width/2,frame.size.height/2);

    //设置饼图 frame

    [_pieView setFrame:frame];

    [_pieView.layer setCornerRadius:_pieRadius];

    CGFloat infoRadius = kInfoRadius;

    frame = CGRectMake(_pieCenter.x – infoRadius, _pieCenter.y –
infoRadius, infoRadius*2, infoRadius*2);

    //设置白色音讯图 frame

    [_infoView setFrame:frame];

    [_infoView.layer setCornerRadius:infoRadius];

}

-(instancetype)initWithFrame:(CGRect)frame withTitle1:(NSString*)title1
withTitle2:(NSString*)title2withTitle3:(NSString*)title3
withTitle4:(NSString*)title4;

从上边的代码,你应当能收看代码原来的逻辑顺序,那样处理代码的目标就是经过在开始化那些控件的时候就依据给定的
frame
宽高取最小值作为饼图的直径,进而确定饼图以及白色新闻图的frame,不过前提我刚说了,一切都创制在
frame 是一个交给的固定值的功底上的。我如此说您或许会认为意外,frame
本来就是一个稳住好的适用的CGRect 啊什么鬼啊是还是不是神经啊
那么怎样状态下您并不知道 frame
到底是稍稍吧?那就是自个儿在初阶化这些控件的时候也用了
AutoLayout,也就是说我在上一个界面就是通过自律来治本这么些控件的布局,那么自己只要求加以约束原则就好并不必要传一个稳住的值来给那个控件,像这么:

-(void)showInView:(UIView*)view;

WS(weakSelf);

_pie = [[PieChart alloc] initWithFrame:CGRectZero];

_pie.dataSource = self;

[self.view addSubview:_pie];

[_pie mas_makeConstraints:^(MASConstraintMaker *make) {

    make.edges.equalTo(weakSelf.view);

}];

@end

就像是你见到的,我在初始化的时候并从未交给一个稳住好的有效的
frame,而是加了一句约束原则,这一句话的趣味就是 pie 的高低要跟当前的
view 一样大,是的,这一切都是自动的。

.m

那么现在我要做的就是让 pie 里面的一对控件也 autoLayout,可是本人依旧要求把
view 的 frame 告诉 pie,因为我本来有一个逻辑是须求取frame
宽高纤维值来作为饼图的直径的,真恼人,那么大家就相应想艺术告诉 pie
我现在的 frame 是多大,你再来做相应调整。好呢,我先来布告 pie:

@implementationHKErrorView

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    _pie.frame = self.view.bounds;

}

-(instancetype)initWithFrame:(CGRect)frame withTitle1:(NSString*)title1
{

那就是说现在自己在 pie 里就要求改了:

if(self= [superinitWithFrame:frame]) {

– (id)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

    if(self){

          self.backgroundColor = [UIColor clearColor];

         WS(weakSelf);

         _scrollContentView = [[UIScrollView alloc]
initWithFrame:self.bounds];

         _scrollContentView.backgroundColor = [UIColor
clearColor];

         [self addSubview:_scrollContentView];

         [_scrollContentView
mas_makeConstraints:^(MASConstraintMaker *make) {

              //重点来了,scrollview 须要跟当前 view 一边大

              make.edges.equalTo(weakSelf);

         }];

         __weak UIScrollView *weakScroll = _scrollContentView;

         _pieView = [[UIView alloc] initWithFrame:self.bounds];

         _pieView.backgroundColor = [UIColor clearColor];

         [_scrollContentView addSubview:_pieView];

         [_pieView mas_makeConstraints:^(MASConstraintMaker *make)
{

              //饼图中央点 X 值要跟 scrollview 一样,尾部要在
scrollview 上留出kMarginY大小的离开

              make.centerX.equalTo(weakScroll.centerX);

             
make.top.equalTo(weakScroll.top).with.offset(kMarginY);

         }];

         __weak UIView *weakPie = _pieView;

         _infoView = [[UIView alloc]
initWithFrame:self.bounds];

         _infoView.backgroundColor = [UIColor whiteColor];

         [_scrollContentView addSubview:_infoView];

         [_infoView mas_makeConstraints:^(MASConstraintMaker
*make) {

              //白色新闻图的中央点 X 值 Y 值都要跟饼图一样

              make.centerX.equalTo(weakPie.centerX);

              make.centerY.equalTo(weakPie.centerY);

         }];

         _descriptionView = [[UIView alloc]
initWithFrame:self.bounds];

         _descriptionView.backgroundColor = [UIColor clearColor];

         [_scrollContentView addSubview:_descriptionView];

         [_descriptionView mas_makeConstraints:^(MASConstraintMaker
*make) {

              //颜色表达 view 的宽度要跟 scrollview
一样,尾部要在饼图底部距离kPieDesSpace的任务

              make.width.equalTo(weakScroll);

             
make.top.equalTo(weakPie.bottom).with.offset(kPieDesSpace);

         }];

   }

    return self;

}

-(void)setFrame:(CGRect)frame{

    [super setFrame:frame];

    self.pieRadius = MIN(self.bounds.size.width/2,
self.bounds.size.height/2) – kMarginX*2;

    _animationArr = [NSMutableArray array];

    self.textRadius = _pieRadius – (_pieRadius-kInfoRadius)/2;

    //注意那里运用的是
update,因为我事先已经对他们设置过约束,我只是想添加新的束缚,假使仍然用make
的话以前的有着约束都会不得要领,即使用 remake
的话就是把后面的附和约束替换掉。因为自身后天曾经明白 frame
了,现在自我得以将她们的高低进行约束了

    [_pieView updateConstraints:^(MASConstraintMaker *make) {

         make.size.mas_equalTo(CGSizeMake(_pieRadius*2,
_pieRadius*2));

    }];

    [_infoView updateConstraints:^(MASConstraintMaker *make) {

         make.size.mas_equalTo(CGSizeMake(kInfoRadius *2,
kInfoRadius *2));

    }];

}

– (void)setPieRadius:(CGFloat)pieRadius{

    _pieRadius = pieRadius;

    [_pieView.layer setCornerRadius:_pieRadius];

    [_infoView.layer setCornerRadius:kInfoRadius];

}

[selfsetupUIWithTitle1:title1withTitle2:nilwithTitle3:nilwithTitle4:nil];

完成。

}

本人一度不想再说什么呀,那几个代码为毛只好一行一行的复制粘贴进去啊!并且我的空格怎么都遗落了啊!我是手动的敲的这一个空格啊!!手都快抽筋了呀啊啊!!!

returnself;

好吧,其实这几个复制粘贴代码这么难用我或者持之以恒起初动一行一行复制粘贴下来空格敲的如此完美,都是因为我的好情人在帮我摘我最欢欣吃的樱桃准备给本人寄过来!我情难自禁哼起歌来~一想到那呀~就让我欢娱~贴一张图馋馋你们哈哈哈!

}

图片 4

-(instancetype)initWithFrame:(CGRect)frame withTitle1:(NSString*)title1
withTitle2:(NSString*)title2 {

噢噢太激动了忘记了总计

if(self= [superinitWithFrame:frame]) {

骨子里一先导用autolayout我是不容的,想到要xb、sb还有合法那么繁琐的说话喔喔还有更加看不懂的象形文字我就全身脑袋疼!不过现在有了那般便利的Masonry,就学来用用吧,毕竟苹果的屏幕大小已经起来不相同等了,用一个恋人的话来说,autolayout是方向。不管怎么样,了然下一而再好的

[selfsetupUIWithTitle1:title1withTitle2:title2withTitle3:nilwithTitle4:nil];

}

returnself;

}

-(instancetype)initWithFrame:(CGRect)frame withTitle1:(NSString*)title1
withTitle2:(NSString*)title2withTitle3:(NSString*)title3 {

if(self= [superinitWithFrame:frame]) {

[selfsetupUIWithTitle1:title1withTitle2:title2withTitle3:title3withTitle4:nil];

}

returnself;

}

-(instancetype)initWithFrame:(CGRect)frame withTitle1:(NSString*)title1
withTitle2:(NSString*)title2withTitle3:(NSString*)title3
withTitle4:(NSString*)title4 {

if(self= [superinitWithFrame:frame]) {

[selfsetupUIWithTitle1:title1withTitle2:title2withTitle3:title3withTitle4:title4];

}

returnself;

}

#pragma mark -设置界面

– (void)setupUIWithTitle1:(NSString*)title1
withTitle2:(NSString*)title2withTitle3:(NSString*)title3
withTitle4:(NSString*)title4{

if(title1 ==nil) {

return;

}

UIView*backgrandView = [[UIViewalloc]init];

backgrandView.backgroundColor= [UIColor
colorWithRed:34.0/255.0green:34.0/255.0blue:34.0/255.0alpha:0.6];

[self addSubview:backgrandView];

[backgrandView mas_makeConstraints:^(MASConstraintMaker*make) {

make.top.bottom.left.right.equalTo(self);

}];

NSInteger labelCount =0;

UIView*showView = [[UIView alloc]init];

showView.backgroundColor= [UIColor whiteColor];

showView.layer.cornerRadius=4.0;

showView.layer.masksToBounds=YES;

[self addSubview:showView];

if(title2 ==nil) {

labelCount =1;

[showView mas_makeConstraints:^(MASConstraintMaker*make) {

make.center.equalTo(self);

make.size.mas_equalTo(CGSizeMake(kUIScreenWidth-60,60));

}];

UILabel*label = [[UILabelalloc]init];

label.text= title1;

label.textColor=[UIColor blueColor];

label.font= [UIFontsystemFontOfSize:16];

label.numberOfLines=0;

label.textAlignment=NSTextAlignmentCenter;

[label sizeToFit];

[showView addSubview:label];

[label mas_makeConstraints:^(MASConstraintMaker*make) {

make.center.equalTo(showView);

make.left.mas_greaterThanOrEqualTo(showView).offset(8);

make.right.mas_greaterThanOrEqualTo(showView).offset(8);

}];

}elseif(title3 ==nil) {

labelCount =2;

[showView mas_makeConstraints:^(MASConstraintMaker*make) {

make.center.equalTo(self);

make.size.mas_equalTo(CGSizeMake(kUIScreenWidth-60,80));

}];

UILabel*label1 = [[UILabel alloc]init];

label1.text= title1;

label1.textColor=[UIColor blueColor];

label1.font= [UIFontsystemFontOfSize:16];

[label1 sizeToFit];

[showView addSubview:label1];

[label1 mas_makeConstraints:^(MASConstraintMaker*make) {

make.centerX.equalTo(showView);

make.centerY.equalTo(showView).offset(-14);

}];

UILabel*label2 = [[UILabel alloc]init];

label2.text= title2;

label2.textColor=[UIColor blueColor];

label2.font= [UIFontsystemFontOfSize:16];

[label2 sizeToFit];

[showView addSubview:label2];

[label2 mas_makeConstraints:^(MASConstraintMaker*make) {

make.centerX.equalTo(label1);

make.top.equalTo(label1.mas_bottom).offset(8);

}];

}elseif(title4 ==nil){

labelCount =3;

[showView mas_makeConstraints:^(MASConstraintMaker*make) {

make.center.equalTo(self);

make.size.mas_equalTo(CGSizeMake(kUIScreenWidth-60,100));

}];

UILabel*label2 = [[UILabel alloc]init];

label2.text= title2;

label2.textColor=KHKTitleColor;

label2.font= [UIFontsystemFontOfSize:16];

[label2sizeToFit];

[showViewaddSubview:label2];

[label2mas_makeConstraints:^(MASConstraintMaker*make) {

make.centerX.equalTo(showView);

make.centerY.equalTo(showView);

}];

UILabel*label1 = [[UILabelalloc]init];

label1.text= title1;

label1.textColor=KHKTitleColor;

label1.font= [UIFontsystemFontOfSize:16];

[label1sizeToFit];

[showViewaddSubview:label1];

[label1mas_makeConstraints:^(MASConstraintMaker*make) {

make.centerX.equalTo(label2);

make.bottom.equalTo(label2.mas_top).offset(-8);

}];

UILabel*label3 = [[UILabelalloc]init];

label3.text= title3;

label3.textColor=KHKTitleColor;

label3.font= [UIFontsystemFontOfSize:16];

[label3sizeToFit];

[showViewaddSubview:label3];

[label3mas_makeConstraints:^(MASConstraintMaker*make) {

make.centerX.equalTo(label2);

make.top.equalTo(label2.mas_bottom).offset(8);

}];

}else{

labelCount =4;

[showViewmas_makeConstraints:^(MASConstraintMaker*make) {

make.center.equalTo(self);

make.size.mas_equalTo(CGSizeMake(kUIScreenWidth-60,120));

}];

UILabel*label1 = [[UILabelalloc]init];

label1.text= title1;

label1.textColor=[UIColor blueColor];

label1.font= [UIFontsystemFontOfSize:16];

[label1 sizeToFit];

[showView addSubview:label1];

[label1mas_makeConstraints:^(MASConstraintMaker*make) {

make.centerX.equalTo(showView);

make.top.equalTo(showView.mas_top).offset(10);

}];

UILabel*label2 = [[UILabel alloc]init];

label2.text= title2;

label2.textColor=[UIColor blueColor];

label2.font= [UIFontsystemFontOfSize:16];

[label2 sizeToFit];

[showView addSubview:label2];

[label2 mas_makeConstraints:^(MASConstraintMaker*make) {

make.centerX.equalTo(label1);

make.top.equalTo(label1.mas_bottom).offset(8);

}];

UILabel*label3 = [[UILabel alloc]init];

label3.text= title3;

label3.textColor=[UIColor blueColor];

label3.font= [UIFontsystemFontOfSize:16];

[label3 sizeToFit];

[showView addSubview:label3];

[label3 mas_makeConstraints:^(MASConstraintMaker*make) {

make.centerX.equalTo(label2);

make.top.equalTo(label2.mas_bottom).offset(8);

}];

UILabel*label4 = [[UILabel alloc]init];

label4.text= title4;

label4.textColor=KHKTitleColor;

label4.font= [UIFontsystemFontOfSize:16];

[label4 sizeToFit];

[showView addSubview:label4];

[label4 mas_makeConstraints:^(MASConstraintMaker*make) {

make.centerX.equalTo(label3);

make.top.equalTo(label3.mas_bottom).offset(8);

}];

}

}

-(void)showInView:(UIView*)view {

[view addSubview:self];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
(int64_t)(2*NSEC_PER_SEC)),dispatch_get_main_queue(), ^{

[UIViewanimateWithDuration:.3animations:^{

self.alpha=0;

}completion:^(BOOLfinished) {

[self removeFromSuperview];

}];

});

}

@end