"天下武功,无快不破",求上天眷顾,愿各位技术大神来打通我的任督二脉,从此启动开挂模式,在技术进阶之路上狂奔.
最近项目开发中用到了Masonry框架
Masonry是一个轻量级的布局框架,拥有自己的描述语法,采用更优雅的链式语法封装自动布局 ,简洁明了,并具有高可读性,而且同时支持 iOS 和 Max OS X。
我们先来看一段官方的sample code来认识一下Masonry
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(superview).with.insets(padding);
}];
相信各位大神早就熟知Masonry 这个开源框架,它主要采用链式编程思想.
链式编程思想:是将多个操作(多行代码)通过点号(.)链接在一起成为一句代码,使代码可读性好。a(1).b(2).c(3)
链式编程特点:方法的返回值是block,block必须有返回值(本身对象),block参数(需要操作的值)
让我们来思考以下代码示例:
UIView *redView = [UIView new];
redView.X(100).Y(20).Width(50).Height(100);
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
UIView *blueView = [UIView new];
blueView.Center(CGPointMake(150, 300)).Size(CGSizeMake(100, 50));
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];
大家有没有对以上代码布局感到奇怪,和我们通常的 frame 布局有所出入,请继续向下浏览
按照链式编程思想,本人对 UIVIew+Frame 这个布局分类做了简单扩展.
源码下载
UIView+Frame.h 头文件
#import <UIKit/UIKit.h>
@interface UIView (Frame)
- (UIView* (^)(CGFloat))X;
- (UIView* (^)(CGFloat))Y;
- (UIView* (^)(CGFloat))Width;
- (UIView* (^)(CGFloat))Height;
- (UIView* (^)(CGFloat))CenterX;
- (UIView* (^)(CGFloat))CenterY;
- (UIView* (^)(CGSize))Size;
@end
UIView+Frame.m
#import "UIView+Frame.h"
@implementation UIView (Frame)
- (UIView* (^)(CGFloat))X
{
return ^UIView*(CGFloat x){
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
return self;
};
}
- (UIView* (^)(CGFloat))Y
{
return ^UIView*(CGFloat y){
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
return self;
};
}
- (UIView* (^)(CGFloat))Width
{
return ^UIView*(CGFloat width){
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
return self;
};
}
- (UIView* (^)(CGFloat))Height
{
return ^UIView*(CGFloat height){
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
return self;
};
}
- (UIView* (^)(CGFloat))CenterX
{
return ^UIView*(CGFloat centerX){
CGRect frame = self.frame;
frame.origin.x = centerX;
self.frame = frame;
return self;
};
}
- (UIView* (^)(CGFloat))CenterY
{
return ^UIView*(CGFloat centerY){
CGRect frame = self.frame;
frame.origin.y = centerY;
self.frame = frame;
return self;
};
}
- (UIView* (^)(CGSize))Size
{
return ^UIView*(CGSize size){
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
return self;
};
}
- (void)setX:(CGFloat)x
{
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}
- (CGFloat)x
{
return self.frame.origin.x;
}
- (void)setY:(CGFloat)y
{
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
}
- (CGFloat)y
{
return self.frame.origin.y;
}
- (void)setWidth:(CGFloat)width
{
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}
- (CGFloat)width
{
return self.frame.size.width;
}
- (void)setHeight:(CGFloat)height
{
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}
- (CGFloat)height
{
return self.frame.size.height;
}
- (CGSize)size{
return self.frame.size;
}
- (void)setSize:(CGSize)size{
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
}
- (CGFloat)centerX{
return self.frame.origin.x;
}
- (void)setCenterX:(CGFloat)centerX{
CGRect frame = self.frame;
frame.origin.x = centerX;
self.frame = frame;
}
- (CGFloat)centerY{
return self.frame.origin.y;
}
- (void)setCenterY:(CGFloat)centerY{
CGRect frame = self.frame;
frame.origin.y = centerY;
self.frame = frame;
}
@end
笔者做为一名屌丝程序员,每天都在技术路上前行,由于本人天生愚钝,学艺不精,在此献丑啦!
项目地址:https://github.com/524429264/LSBC-Demo.git