本人有若干成套学习视频, 可试看! 可试看! 可试看, 重要的事情说三遍 包含Java
, 数据结构与算法
, iOS
, 安卓
, python
, flutter
等等, 如有需要, 联系微信tsaievan
.
Masonry想必大家都用过, 今天带大家来稍微看一下内部实现的代码:
首先我写这么一段代码:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *yfView = [[UIView alloc] init];
yfView.backgroundColor = [UIColor cyanColor];
[self.view addSubview:yfView];
[yfView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.equalTo(@50);
make.bottom.right.equalTo(@(-50));
}];
}
实现这样一个效果, so easy对不对?
那么mas_makeConstraints:
这个方法内部做了些什么呢?
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block {
self.translatesAutoresizingMaskIntoConstraints = NO;
MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
block(constraintMaker);
return [constraintMaker install];
}
- 首先将
translatesAutoresizingMaskIntoConstraints
这个属性改成NO
, 首先, 苹果官方是这么说的:
By default, the autoresizing mask on a view gives rise to constraints that fully determine
the view's position. This allows the auto layout system to track the frames of views whose
layout is controlled manually (through -setFrame:, for example).
When you elect to position the view using auto layout by adding your own constraints,
you must set this property to NO. IB will do this for you.
如果你要使用自动布局, 这个属性就要改成NO
, stroryBoard已经帮你修改了这个属性.
-
创建一个maker对象, 丢到block里面去, 供外界使用. 所以, 大家用的
make...
, 就是在这个时候创建的.- 创建的时候, 将调用者
view
传给make
的属性view
- 同时, 创建一个可变数组, 用来保存所有的约束对象
- 创建的时候, 将调用者
- (id)initWithView:(MAS_VIEW *)view {
self = [super init];
if (!self) return nil;
self.view = view;
self.constraints = NSMutableArray.new;
return self;
}
执行block
安装约束
- (void)install {
if (self.hasBeenInstalled) {
return;
}
if ([self supportsActiveProperty] && self.layoutConstraint) {
self.layoutConstraint.active = YES;
[self.firstViewAttribute.view.mas_installedConstraints addObject:self];
return;
}
MAS_VIEW *firstLayoutItem = self.firstViewAttribute.item;
NSLayoutAttribute firstLayoutAttribute = self.firstViewAttribute.layoutAttribute;
MAS_VIEW *secondLayoutItem = self.secondViewAttribute.item;
NSLayoutAttribute secondLayoutAttribute = self.secondViewAttribute.layoutAttribute;
// alignment attributes must have a secondViewAttribute
// therefore we assume that is refering to superview
// eg make.left.equalTo(@10)
if (!self.firstViewAttribute.isSizeAttribute && !self.secondViewAttribute) {
secondLayoutItem = self.firstViewAttribute.view.superview;
secondLayoutAttribute = firstLayoutAttribute;
}
MASLayoutConstraint *layoutConstraint
= [MASLayoutConstraint constraintWithItem:firstLayoutItem
attribute:firstLayoutAttribute
relatedBy:self.layoutRelation
toItem:secondLayoutItem
attribute:secondLayoutAttribute
multiplier:self.layoutMultiplier
constant:self.layoutConstant];
layoutConstraint.priority = self.layoutPriority;
layoutConstraint.mas_key = self.mas_key;
if (self.secondViewAttribute.view) {
MAS_VIEW *closestCommonSuperview = [self.firstViewAttribute.view mas_closestCommonSuperview:self.secondViewAttribute.view];
NSAssert(closestCommonSuperview,
@"couldn't find a common superview for %@ and %@",
self.firstViewAttribute.view, self.secondViewAttribute.view);
self.installedView = closestCommonSuperview;
} else if (self.firstViewAttribute.isSizeAttribute) {
self.installedView = self.firstViewAttribute.view;
} else {
self.installedView = self.firstViewAttribute.view.superview;
}
MASLayoutConstraint *existingConstraint = nil;
if (self.updateExisting) {
existingConstraint = [self layoutConstraintSimilarTo:layoutConstraint];
}
if (existingConstraint) {
// just update the constant
existingConstraint.constant = layoutConstraint.constant;
self.layoutConstraint = existingConstraint;
} else {
[self.installedView addConstraint:layoutConstraint];
self.layoutConstraint = layoutConstraint;
[firstLayoutItem.mas_installedConstraints addObject:self];
}
}
上面这一大坨恶心的代码框架已经帮你写好了, 所以我们在外界调用的时候非常的方便.
但这不是今天链式编程的重点, 我们需要看的是:
make.top.left.equalTo(@50);
make.bottom.right.equalTo(@(-50));
这段代码里发生了什么?
这段代码里频繁使用了点语法
- 点语法的本质其实是
get
方法, (在这里可以这么理解!)
- 在这个里面, get方法不仅仅添加了约束, 还将自身返回了出去
- (MASConstraint *)constraint:(MASConstraint *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
MASViewAttribute *viewAttribute = [[MASViewAttribute alloc] initWithView:self.view layoutAttribute:layoutAttribute];
MASViewConstraint *newConstraint = [[MASViewConstraint alloc] initWithFirstViewAttribute:viewAttribute];
if ([constraint isKindOfClass:MASViewConstraint.class]) {
//replace with composite constraint
NSArray *children = @[constraint, newConstraint];
MASCompositeConstraint *compositeConstraint = [[MASCompositeConstraint alloc] initWithChildren:children];
compositeConstraint.delegate = self;
[self constraint:constraint shouldBeReplacedWithConstraint:compositeConstraint];
return compositeConstraint;
}
if (!constraint) {
newConstraint.delegate = self;
[self.constraints addObject:newConstraint];
}
return newConstraint;
}
这样, 因为get方法是有返回值的, 返回值又是自身, 所以可以一直点下去.
话说到这里, 有一个疑问:
make.top.left.equalTo(@50)
- 这句代码之后, 还可以点语法点下去吗? 我们来看看
equalTo
是个什么东东:
- (MASConstraint * (^)(id))equalTo {
return ^id(id attribute) {
return self.equalToWithRelation(attribute, NSLayoutRelationEqual);
};
}
首先, equalTo
依然是一个get
方法, 这个get
方法的返回值是一个block
, 这个方法的内部不仅仅设置约束, 还有一个返回值,返回值block
直接在外界加一个括号()
调用了, 调用之后, 这个block
是一个参数为id类型, 返回值为MASConstraint *
类型的, 这样, 在调用block
之后, 又得到一个MASConstraint *
对象, 就又可以使用点语法, 一直点下去.
equalTo
方法中, 返回一大段block
- (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation {
return ^id(id attribute, NSLayoutRelation relation) {
if ([attribute isKindOfClass:NSArray.class]) {
NSAssert(!self.hasLayoutRelation, @"Redefinition of constraint relation");
NSMutableArray *children = NSMutableArray.new;
for (id attr in attribute) {
MASViewConstraint *viewConstraint = [self copy];
viewConstraint.layoutRelation = relation;
viewConstraint.secondViewAttribute = attr;
[children addObject:viewConstraint];
}
MASCompositeConstraint *compositeConstraint = [[MASCompositeConstraint alloc] initWithChildren:children];
compositeConstraint.delegate = self.delegate;
[self.delegate constraint:self shouldBeReplacedWithConstraint:compositeConstraint];
return compositeConstraint;
} else {
NSAssert(!self.hasLayoutRelation || self.layoutRelation == relation && [attribute isKindOfClass:NSValue.class], @"Redefinition of constraint relation");
self.layoutRelation = relation;
self.secondViewAttribute = attribute;
return self;
}
};
}
下面,我们利用这种链式编程思想, 自己写一个加法小工具, 任何对象都可以利用这个小工具进行计算, 代码很简单, 关键是体会一下这种编程思想:
- 首先, 我建一个分类, 在分类中暴露这样一个方法:
@interface NSObject (Add)
- (int)yf_add:(void(^)(YFSumTool *tool))addBlock;
这里面我提供了一个tool, 相当于Masonry中的make.
- 然后提供两个
加
和减
的get
方法
- (NSObject * (^)(int number))minus;
- (NSObject * (^)(int number))plus;
- (int)yf_add:(void(^)(YFSumTool *tool))addBlock {
YFSumTool *tool = [[YFSumTool alloc] init];
addBlock(tool);
return sum;
}
- 在这个方法中, 先创建一个
tool
对象作为参数传到block里面去供外界使用, 然后, 调用block, 最后返回结果.
加
和减
的get
方法实现如下:
- (NSObject * (^)(int number))plus {
return ^(int number) {
sum += number;
return self;
};
}
- (NSObject * (^)(int number))minus {
return ^(int number) {
sum -= number;
return self;
};
}
-
get
方法的返回值是block
, 这个block
有一个int
类型的参数, 返回值为NSObject
类型, 当这个block
被调用时, 将你需要加或者减的数字作为参数传进去, 并将自身self
返回出来, 这样下次可以继续调用, 点语法可以一直用下去: