用 Masonry 時,你一定寫過這樣的程式碼:
[self.view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(50);
make.right.mas_equalTo(-50);
make.top.mas_equalTo(50);
make.bottom.mas_equalTo(-50);
}];
這裡 view1 是 UIView,呼叫的 mas_makeConstraints 寫在分類裡。來看它的實作:
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block {
self.translatesAutoresizingMaskIntoConstraints = NO;
MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
block(constraintMaker);
return [constraintMaker install];
}
短短幾行,做了三件事:
第一,關掉 autoresizing 轉換。把 translatesAutoresizingMaskIntoConstraints 設為 NO,這是用 Auto Layout 的前提。
第二,建立 maker。初始化一個 MASConstraintMaker,來看看它的 init 做了什麼:
- (id)initWithView:(MAS_VIEW *)view {
self = [super init];
if (!self) return nil;
self.view = view;
self.constraints = NSMutableArray.new;
return self;
}
很單純:把當前 view 存起來,並準備一個約束陣列。
第三,執行 block 並安裝。回頭看 block(constraintMaker); 這一行——很多人會誤以為這是什麼語法糖,其實它就是一次普通的函式呼叫,後面那一大塊只是傳進去的參數,只不過這個參數剛好是一個 block。
為了避免混淆,我們把參數名稱改清楚一點:
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))constraintMakerblock {
self.translatesAutoresizingMaskIntoConstraints = NO;
MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
constraintMakerblock(constraintMaker);
return [constraintMaker install];
}
也就是說,上面那段語法糖完全等價於下面這串直白寫法:
self.view1.translatesAutoresizingMaskIntoConstraints = NO;
MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self.view1];
constraintMaker.left.mas_equalTo(50);
constraintMaker.right.mas_equalTo(-50);
constraintMaker.top.mas_equalTo(50);
constraintMaker.bottom.mas_equalTo(-50);
[constraintMaker install];
顯然可以,只是好不好用是另一回事。我們一步步試。
嘗試一:直接封裝成一個方法。
- (void)addMakeConstraints {
// 初始化
self.translatesAutoresizingMaskIntoConstraints = NO;
MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
// 設定屬性 ...
// 安裝
[constraintMaker install];
}
問題來了:中間「設定屬性」那段要怎麼把值傳進來?參數太多了。
嘗試二:用字典傳值。
- (void)addMakeConstraints:(NSMutableDictionary *)dictionary {
self.translatesAutoresizingMaskIntoConstraints = NO;
MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
constraintMaker.left.mas_equalTo([dictionary[@"left"] intValue]);
constraintMaker.right.mas_equalTo([dictionary[@"right"] intValue]);
constraintMaker.top.mas_equalTo([dictionary[@"top"] intValue]);
constraintMaker.bottom.mas_equalTo([dictionary[@"bottom"] intValue]);
[constraintMaker install];
}
NSMutableDictionary *constraint = [NSMutableDictionary alloc] init...;
[self.view1 addMakeConstraints:constraint];
能跑,但字串 key 缺乏型別檢查,寫起來也囉嗦,顯然不是好主意。
嘗試三:用 delegate。回頭看嘗試一的骨架,我們缺的其實只是「設定屬性」那一段。把它交給外部物件:
- (void)addMakeConstraints {
self.translatesAutoresizingMaskIntoConstraints = NO;
MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
[self.del addConstraint:constraintMaker];
[constraintMaker install];
}
// 外部實作
- (void)addConstraint:(MASConstraintMaker *)maker {
maker.left.mas_equalTo(50);
maker.right.mas_equalTo(-50);
maker.top.mas_equalTo(50);
maker.bottom.mas_equalTo(-50);
}
嘗試四:改用 block 當屬性。既然 delegate 可以,block 當然也行。
- (void)addMakeConstraints {
self.translatesAutoresizingMaskIntoConstraints = NO;
MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
self.makerBlock(constraintMaker);
[constraintMaker install];
}
[self.view1 addMakeConstraints];
self.view1.makerBlock = ^(MASConstraintMaker *maker) {
maker.left.mas_equalTo(50);
maker.right.mas_equalTo(-50);
maker.top.mas_equalTo(50);
maker.bottom.mas_equalTo(-50);
};
嘗試五:直接把 block 當參數傳。這就是 Masonry 最終採用的形式。
- (void)addMakeConstraints:(void(^)(MASConstraintMaker *maker))maker {
self.translatesAutoresizingMaskIntoConstraints = NO;
MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
maker(constraintMaker);
[constraintMaker install];
}
[self.view1 addMakeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(50);
make.right.mas_equalTo(-50);
make.top.mas_equalTo(50);
make.bottom.mas_equalTo(-50);
}];
一句話總結:建立 MASConstraintMaker → 設定屬性 → 安裝,唯一的巧思是把「設定屬性」那段以 block 的形式交回呼叫方撰寫。
接著看 View+MASAdditions 這個分類:
@property (nonatomic, strong, readonly) MASViewAttribute *mas_left;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_top;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_right;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_bottom;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_leading;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_trailing;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_width;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_height;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerX;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerY;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_baseline;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_attribute; // 帶參數版本
光看名字就知道,這一段是把 NSLayoutAttribute 列舉包裝成物件。你可能會問:NSLayoutAttribute 不就只是一堆列舉值嗎,包成物件有意義嗎?
看這段實際寫法就明白了:
[self.view2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.view1.mas_bottom).offset(10);
}];
這裡出現了 self.view1.mas_bottom。若用原生 API 表達同一件事,你得這樣寫:
NSLayoutConstraint *constaint =
[NSLayoutConstraint constraintWithItem:self
attribute:att
relatedBy:NSLayoutRelationEqual
toItem:attribute.view
attribute:attribute.attribute
multiplier:1.0
constant:space];
注意其中的 toItem——要設定它,你必須知道「對面那個 view 是誰」。而單靠一個 NSLayoutAttribute 列舉值,是拿不到 view 參照的。
這就是 MASViewAttribute 存在的意義:它在包裝 attribute 的同時,額外保留了 view 屬性。名字唸起來就是 MAS - View - Attribute,這樣 toItem 才能被正確帶出來。
// 新增約束
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))constraintMakerblock;
// 更新約束(已存在的約束會被更新而非新增)
- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;
// 移除既有約束後重建
- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;
三者流程一致,差別只在兩個開關:
constraintMaker.updateExisting = YES;
constraintMaker.removeExisting = YES;
回到第二步的 make.left.mas_equalTo(50)。這裡的關鍵是:make 的 left/top/right/bottom 等屬性回傳的是 MASConstraint 型別,而 mas_equalTo 的回傳值是一個 block:
- (MASConstraint * (^)(id attr))mas_equalTo;
- (MASConstraint * (^)(id attr))mas_greaterThanOrEqualTo;
- (MASConstraint * (^)(id attr))mas_lessThanOrEqualTo;
- (MASConstraint * (^)(id))mas_equalTo {
return ^id(id attribute) {
return self.equalToWithRelation(attribute, NSLayoutRelationEqual);
};
}
也就是說 mas_equalTo(50) 呼叫完之後,回傳的是 MASConstraint *,所以能繼續串 .offset(10)、.priorityHigh() 這些方法。這就是整條鏈式語法能寫成一行、而且從左到右讀起來像英文句子的原因。