核心数据结构
- MASViewAttribute
MASViewAttribute存储了UIView及其布局属性(NSLayoutAttribute),用于约束等式的左部分或右部分。
@interface MASViewAttribute : NSObject
@property (nonatomic, weak, readonly) MAS_VIEW *view;
@property (nonatomic, weak, readonly) id item;
@property (nonatomic, assign, readonly) NSLayoutAttribute layoutAttribute;
@end
- MASConstraint,
MASConstraint可以表示单个布局约束(MASViewConstraint),也可以表示多个布局约束(MASCompositeConstraint)。MASConstraint支持用链式语法创建新的MASConstraint或者修改MASConstraint自身的一些属性。
3.MASViewConstraint继承自MASConstraint,表示单个布局约束。
@interface MASViewConstraint : MASConstraint <NSCopying>
@property (nonatomic, strong, readonly) MASViewAttribute *firstViewAttribute;
@property (nonatomic, strong, readonly) MASViewAttribute *secondViewAttribute;
@property (nonatomic, assign) NSLayoutRelation layoutRelation;
@property (nonatomic, assign) MASLayoutPriority layoutPriority;
@property (nonatomic, assign) CGFloat layoutMultiplier;
@property (nonatomic, assign) CGFloat layoutConstant;
@end
4.MASCompositeConstraint继承自MASConstraint,表示多个布局约束,其实现就是MASViewConstraint的数组容器。
@interface MASCompositeConstraint () <MASConstraintDelegate>
@property (nonatomic, strong) NSMutableArray *childConstraints;
@end
4.MASConstraintMaker
MASConstraintMaker用来创建并存储MASConstraint。
@interface MASConstraintMaker () <MASConstraintDelegate>
@property (nonatomic, weak) MAS_VIEW *view;
@property (nonatomic, strong) NSMutableArray *constraints;
@end
使用分析
示例代码:
[superView addSubview:someView];
[someView mas_makeConstraints:^(MASConstraintMaker *make) { //c1
make.left.right.top.equalTo(superView).offset(100); //c2
make.height.equalTo(@100); //c3
}];
c1语句的内部实现如下
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block {
self.translatesAutoresizingMaskIntoConstraints = NO;
//创建MASConstraintMaker对象
MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
//调用传入的block,block内部会创建若干个MASConstraint,并添加到constraintMaker的constraints数组里。
block(constraintMaker);
//安装constraints数组中的约束,即创建若干个NSLayoutContraint,并添加到合适的UIView上。
return [constraintMaker install];
}
c2语句调用的一系列方法内部实现如下,可以发现以下方法返回的对象是MASConstraint对象或者是Block对象(block返回的对象是MASConstraint对象),这正是链式编程的思想,可以通过点‘.’来连接不同的函数调用 。
@implementation MASConstraintMaker
- (MASConstraint *)left {
return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeft];
}
end
@implementation MASConstraint
#pragma mark - Chaining
- (MASConstraint *)right {
return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRight];
}
- (MASConstraint *)top {
return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTop];
}
- (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute __unused)layoutAttribute {
MASMethodNotImplemented();
}
#pragma mark - NSLayoutRelation proxies
- (MASConstraint * (^)(id))equalTo {
return ^id(id attribute) {
return self.equalToWithRelation(attribute, NSLayoutRelationEqual);
};
}
#pragma mark - NSLayoutConstraint constant proxies
- (MASConstraint * (^)(CGFloat))offset {
return ^id(CGFloat offset){
self.offset = offset;
return self;
};
}
@end
按照上面的内部实现,我们可以将示例代码展开整理为:
- (void)createViews {
[superView addSubview:someView];
[someView mas_makeConstraints:^(MASConstraintMaker *make) {
//创建一个视图约束leftViewConstraint,并将约束添加到maker.constraints中,最后返回leftViewConstraint。
MASConstraint *leftViewConstraint = make.left;
//创建一个视图约束rightViewConstraint,一个组合约束compositeConstraint,将leftViewConstraint和rightViewConstraint加到compositeConstraint1.childConstraints中,再将maker.constraints中的leftViewConstraint替换成compositeConstraint,最后返回compositeConstraint。
MASConstraint *compositeConstraint = leftViewConstraint.right;
//创建一个视图约束topViewConstraint,将其添加到compositeConstraint.childConstraints,最后返回compositeConstraint。
compositeConstraint = compositeConstraint.top;
//设置compositeConstraint.childConstraints中的每个视图约束的secondViewAttribute。
compositeConstraint = compositeConstraint.equalTo(superView);
//设置compositeConstraint.childConstraints中的每个视图约束的layoutConstant。
compositeConstraint = compositeConstraint.offset(100);
//创建一个视图约束heightViewConstraint,并将约束添加到maker.constraints中,最后返回heightViewConstraint。
MASConstraint *heightViewConstraint = make.height;
//设置heightViewConstraint的layoutConstant,并返回heightViewConstraint。
heightViewConstraint = heightViewConstraint.equalTo(@100);
//最后maker.constraints中有两个元素:compositeConstraint和heightViewConstraint。
}];
}