首先总结一下心得:我发现看Massonry的框架,并不像看AFNetworking SDWebImage 那样好看,如果对 block 不是很了解的话,完全不知道在写什么,而Massonry 先看外面的方法,再看里面的内容,比如说
[yellowView mas_makeConstraints:^(MASConstraintMaker *make) {
//描述所有约束
make.left.top.equalTo(@20);
make.right.bottom.equalTo(@(-20));
}];
第一眼看的时候就知道这是block语法,点mas_makeConstraints
这个方法进去,跳这里
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block {
//取消自动布局
self.translatesAutoresizingMaskIntoConstraints = NO;
//MASConstraintMaker 是约束制造者 self:是你添加约束的view 在这里就相当于yellowView
MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc]
initWithView:self];
//把所有的控件的约束全部保存到约束制造者
block(constraintMaker);
//遍历约束制造者的所有约束 给控件添加约束
return [constraintMaker install];
}
block(constraintMaker);调用block 也就是外部的代码
这个就是相当于调用了
void(^block)(MASConstraintMaker *maker) = ^(MASConstraintMaker *make) {
make.left.top.equalTo(@10);
make.right.bottom.equalTo(@(-10));
};
[redView mas_makeConstraints:block];
在这里block 的作用就是 保存一份代码,我们再看看[constraintMaker install]
- (NSArray *)install {
if (self.removeExisting) {
//这是所有约束的数组
NSArray *installedConstraints = [MASViewConstraint installedConstraintsForView:self.view];
//遍历所有的约束
for (MASConstraint *constraint in installedConstraints) {
//移除
[constraint uninstall];
}
}
//遍历所有的约束,并安装
NSArray *constraints = self.constraints.copy;
for (MASConstraint *constraint in constraints) {
constraint.updateExisting = self.updateExisting;
[constraint install];
}
[self.constraints removeAllObjects];
return constraints;
}
再点[constraint install]
- (void)install {
if (self.hasBeenInstalled) {
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;
}
// 把约束保存到 MASViewConstraint 其余的可能我们可能看不懂 只需要知道 mas_makeConstraints 方法里做的事情就可以
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];
}
}
现在我们知道为什么 mas_makeConstraints 可以给空间添加约束了把,我们继续看block里面的东西
make.left.top.equalTo(@20);
make.right.bottom.equalTo(@(-20));
先看make.left 这是一个get 方法,点进去
- (MASConstraint *)left {
//self 是约束制造者 也就是MASConstraintMaker
return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeft];
}
- (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
return [self constraint:nil addConstraintWithLayoutAttribute:layoutAttribute];
}
- (MASConstraint *)constraint:(MASConstraint *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
//self.view 是给哪个控件添加约束 也就是 yellowView
MASViewAttribute *viewAttribute = [[MASViewAttribute alloc] initWithView:self.view layoutAttribute:layoutAttribute];
//添加控件约束类 newConstraint一个新的约束 只是说给哪个控件添加了 并没有描述值
MASViewConstraint *newConstraint = [[MASViewConstraint alloc] initWithFirstViewAttribute:viewAttribute];
//判断constraint是什么类型 由于上面的方法 返回的是 nil 不走if里面的
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) {
//这里的self 指向MASConstraintMaker
newConstraint.delegate = self;
//self.constraints 所有约束的数组 这是在[constraintMaker install]这添加的约束
[self.constraints addObject:newConstraint];
}
return newConstraint;
}
到这里 make.left 指向的是MASViewConstraint 约束类 而不是MASConstraintMaker(约束制造者类)下面我们看 top 点top 会出现2个类
[MASViewConstraint top]
[MASConstraintMaker top]
由于make.left 是指向MASViewConstraint 所以我们应该看[MASViewConstraint top] 这个类里的top
- (MASConstraint *)top {
return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTop];
}然后我们看MASViewConstraint里面的方法
- (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
NSAssert(!self.hasLayoutRelation, @"Attributes should be chained before defining the constraint relation");
return [self.delegate constraint:self addConstraintWithLayoutAttribute:layoutAttribute];
}
之前我们已经看过 delegate指向 MASConstraintMaker 所以我们看 MASConstraintMaker 里的方法
- (MASConstraint *)constraint:(MASConstraint *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
// self.view -> redView
// 1.给哪个view添加约束
MASViewAttribute *viewAttribute = [[MASViewAttribute alloc] initWithView:self.view layoutAttribute:layoutAttribute];
// 2.控件约束类
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;
}
又来到了这个方法 把约束添加到数组中 也就是把 make.left.top 左边和顶部的约束全部保存到make.contrains 这时候还没有值 只是描述给哪个控件添加约束 调用equalTo 是添加值
点进去看看equalTo里有什么
//首先它是一个方法 把block作为一个返回值 所以我才写出equalTo(@20) 这种代码
- (MASConstraint * (^)(id))equalTo {
return ^id(id attribute) {
return self.equalToWithRelation(attribute, NSLayoutRelationEqual);
};
}
如果不知道block可以作为返回值的话,一看这个就懵了
return ^id(id attribute) {
return self.equalToWithRelation(attribute, NSLayoutRelationEqual);
};
相当于这么写
void(^block)(MASConstraintMaker *maker) = ^(MASConstraintMaker *make) {
id(^block)(id attribute) = ^id(id attribute) {
return self.equalToWithRelation(attribute, N
SLayoutRelationEqual);
return nil;
};
block(@10);
// equalTo(@10)返回值 = self.equalToWithRelation(attribute, NSLayoutRelationE qual);
make.left.top.equalTo(@10);
make.right.bottom.equalTo(@(-10));
};
make.left.top.equalTo 就是 block,这是block里面嵌套了一个block
self.equalToWithRelation(attribute, NSLayoutRelationEqual);看到这句话我们知道self.equalToWithRelation 也是一个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.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;
}
};
}
self.equalToWithRelation()其实就是调用的if()那堆代码
其实Massonry 的主要核心思想就是 链式编程思想 就是一个函数的返回值是block。可以不断的“点”下去