Masonry源碼分析一:MASConstraintMaker

MASConstraintMaker是Masonry框架運(yùn)轉(zhuǎn)的核心對(duì)象之一,我們知道,Masonry是基于AutoLayout技術(shù)的封裝,所以我們來(lái)探尋一下MASConstraintMaker是如何發(fā)揮作用的。

先看一段典型的用法介紹:

[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(superview.mas_top).with.offset(padding.top); //with is an optional semantic filler
    make.left.equalTo(superview.mas_left).with.offset(padding.left);
    make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom);
    make.right.equalTo(superview.mas_right).with.offset(-padding.right);
}];

第一部分 MASConstraintMaker與AutoLayout關(guān)系梳理

一、我們常用的 mas_makeConstraints方法的實(shí)現(xiàn)

- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block {
    self.translatesAutoresizingMaskIntoConstraints = NO;
    MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
    block(constraintMaker);
    return [constraintMaker install];
}

方法內(nèi)部創(chuàng)建 MASConstraintMaker對(duì)象,然后傳遞到block中,執(zhí)行完block,調(diào)用 [constraintMaker install]來(lái)確保約束被添加到視圖中去。

二、上面MASConstraintMaker對(duì)象的[constraintMaker install]方法

for (MASConstraint *constraint in constraints) {
        constraint.updateExisting = self.updateExisting;
        [constraint install];
    }

可以看到,MASConstraintMaker包含很多的MASConstraint對(duì)象,然后逐一調(diào)用MASConstraint對(duì)象的install方法。

MASViewConstraint是MASConstraint的子類,創(chuàng)建NSLayoutConstraint添加到視圖中去,看一下install方法:

...
MASLayoutConstraint *layoutConstraint
        = [MASLayoutConstraint constraintWithItem:firstLayoutItem
                                        attribute:firstLayoutAttribute
                                        relatedBy:self.layoutRelation
                                           toItem:secondLayoutItem
                                        attribute:secondLayoutAttribute
                                       multiplier:self.layoutMultiplier
                                         constant:self.layoutConstant];

...
...
...
[self.installedView addConstraint:layoutConstraint];

第二部分 查看MASConstraintMaker頭文件

一、頭文件第一部分

@property (nonatomic, strong, readonly) MASConstraint *left;
@property (nonatomic, strong, readonly) MASConstraint *top;
@property (nonatomic, strong, readonly) MASConstraint *right;
@property (nonatomic, strong, readonly) MASConstraint *bottom;
@property (nonatomic, strong, readonly) MASConstraint *leading;
@property (nonatomic, strong, readonly) MASConstraint *trailing;
@property (nonatomic, strong, readonly) MASConstraint *width;
@property (nonatomic, strong, readonly) MASConstraint *height;
@property (nonatomic, strong, readonly) MASConstraint *centerX;
@property (nonatomic, strong, readonly) MASConstraint *centerY;
@property (nonatomic, strong, readonly) MASConstraint *baseline;

#if TARGET_OS_IPHONE

@property (nonatomic, strong, readonly) MASConstraint *leftMargin;
@property (nonatomic, strong, readonly) MASConstraint *rightMargin;
@property (nonatomic, strong, readonly) MASConstraint *topMargin;
@property (nonatomic, strong, readonly) MASConstraint *bottomMargin;
@property (nonatomic, strong, readonly) MASConstraint *leadingMargin;
@property (nonatomic, strong, readonly) MASConstraint *trailingMargin;
@property (nonatomic, strong, readonly) MASConstraint *centerXWithinMargins;
@property (nonatomic, strong, readonly) MASConstraint *centerYWithinMargins;

#endif

上面這些是比較常規(guī)的一些屬性,在一開(kāi)始的例子中

make.top.equalTo(superview.mas_top).with.offset(padding.top);

1、make.top??-->??返回一個(gè)MASConstraint對(duì)象</br>
2、top.equalTo??-->??返回一個(gè)block對(duì)象,equalTo對(duì)應(yīng)的AutoLayout關(guān)系就是NSLayoutRelationEqual,block的接收參數(shù)是id類型,block返回值是MASConstraint類型</br>
3、equalTo(superview.mas_top)??-->??調(diào)用block,返回MASConstraint類型對(duì)象</br>
4、with可以忽略,offset類似,這里不詳解MASConstraint對(duì)象

二、頭文件第二部分

@property (nonatomic, strong, readonly) MASConstraint *(^attributes)(MASAttribute attrs);

@property (nonatomic, strong, readonly) MASConstraint *edges;

@property (nonatomic, strong, readonly) MASConstraint *size;

@property (nonatomic, strong, readonly) MASConstraint *center;

1、attributes??-->??返回一個(gè)block對(duì)象,block的接收參數(shù)是MASAttribute類型,返回MASCompositeConstraint對(duì)象</br>
2、edges??-->??返回一個(gè)MASConstraint對(duì)象,同時(shí)包含了上下左右的布局信息</br>
3、size??-->??返回一個(gè)MASConstraint對(duì)象,同時(shí)包含了寬高的布局信息</br>
4、center??-->??返回一個(gè)MASConstraint對(duì)象,同時(shí)包含了centerX和centerY信息

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容