假如您的花色中还没有 梅森ry,所需框架Masonry

先前自作者曾有两遍专门查阅过 AutoLayout
的连带质地,好啊其实就是部分牛牛在投机的博客揭橥的使用教程及片段心得。一贯都想着试一下然而一向从未什么样时间来执行,那篇文章紧要说的是本身是怎么将
Masonry 移植到原有代码中。

如下:

假若您的品种中还不曾 Masonry,你可以透过第1方治本平台 Cocoapods
来下载,若是有趣味的话,可以看一下本人以前整理的有关 Cocoapods
下载与安装的稿子: IOS依赖管理 –
CocoaPods
.

图片 1

率先简单介绍一下 Masonry,Masonry
是一款能够让开发者十三分便于接纳iOS的自动布局(AutoLayout)机制。Masonry提供越来越周详、友好的API来代替直接采纳NSLayoutConstraint进行编程,可以使视图布局的进度越是自在。(可以吗小编肯定那句话是自作者拷贝过来的,那不是根本)

图片 2

而自小编对 Masonry
最初的认识是因而那篇文章:Masonry介绍与利用举行:神速上手Autolayout –
 CocoaChina 苹果支付汉语站 – 最热的三星开发社区 最热的苹果开发社区
最热的平板电脑开发社区
,并且自个儿意识网络上流传的大部有关
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
本来就是2个恒定好的适当的CGRect 啊什么鬼啊是或不是神经啊
那么如何动静下您并不知道 frame
到底是多少吧?那就是自己在先导化那个控件的时候也用了
AutoLayout,也等于说小编在上三个界面就是经过自律来管理这一个控件的布局,那么本身只须求加以约束原则就好并不必要传3个恒定的值来给这么些控件,像这么:

-(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

相关文章