版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2018.07.20 |
前言
我們做APP界面史飞,也就是布局UI尖昏,那么關(guān)于布局,我們有很多方法构资,蘋果也都提供了支持抽诉,市場上我們用的并不是系統(tǒng)提供原生的layout,對于OC語言一般都是使用一個第三方的布局框架 —— Masonry吐绵。接下來幾篇我們就一起深入看一下這個框架迹淌。感興趣的看上面幾篇文章。
1. Masonry框架詳細解析(一) —— 基本概覽(一)
2. Masonry框架詳細解析(二) —— 基本結(jié)構(gòu)API和約束入口(一)
3. Masonry框架詳細解析(三) —— MASConstraintMaker工廠類(一)
4. Masonry框架詳細解析(四) —— MASConstraint類解析(一)
5. Masonry框架詳細解析(五) —— MASCompositeConstraint類解析(一)
API
該類是一個單約束己单,包含創(chuàng)建NSLayoutConstraint所需要的屬性唉窃,并將其添加到適合的view上,首先看一下API
/**
* A single constraint.
* Contains the attributes neccessary for creating a NSLayoutConstraint and adding it to the appropriate view
*/
@interface MASViewConstraint : MASConstraint <NSCopying>
/**
* First item/view and first attribute of the NSLayoutConstraint
*/
//NSLayoutConstraint的第一個item/view和第一個屬性
@property (nonatomic, strong, readonly) MASViewAttribute *firstViewAttribute;
/**
* Second item/view and second attribute of the NSLayoutConstraint
*/
//NSLayoutConstraint的第二個item/view和第二個屬性
@property (nonatomic, strong, readonly) MASViewAttribute *secondViewAttribute;
/**
* initialises the MASViewConstraint with the first part of the equation
*
* @param firstViewAttribute view.mas_left, view.mas_width etc.
*
* @return a new view constraint
*/
//利用等式的第一部分初始化MASViewConstraint
//參數(shù) firstViewAttribute:view.mas_left, view.mas_width等
- (id)initWithFirstViewAttribute:(MASViewAttribute *)firstViewAttribute;
/**
* Returns all MASViewConstraints installed with this view as a first item.
*
* @param view A view to retrieve constraints for.
*
* @return An array of MASViewConstraints.
*/
//返回該view作為第一個item安裝的所有的MASViewConstraints約束
//參數(shù):view荷鼠,一個用于檢索約束的視圖
+ (NSArray *)installedConstraintsForView:(MAS_VIEW *)view;
@end
下面就簡單的看一下這幾個方法和屬性
1. 實例化方法
實例化方法指的是下面這個方法
- (id)initWithFirstViewAttribute:(MASViewAttribute *)firstViewAttribute;
下面看一下實現(xiàn)
@property (nonatomic, assign) MASLayoutPriority layoutPriority;
@property (nonatomic, assign) CGFloat layoutMultiplier;
- (id)initWithFirstViewAttribute:(MASViewAttribute *)firstViewAttribute {
self = [super init];
if (!self) return nil;
_firstViewAttribute = firstViewAttribute;
self.layoutPriority = MASLayoutPriorityRequired;
self.layoutMultiplier = 1;
return self;
}
2. 返回所有安裝的約束
+ (NSArray *)installedConstraintsForView:(MAS_VIEW *)view;
+ (NSArray *)installedConstraintsForView:(MAS_VIEW *)view {
return [view.mas_installedConstraints allObjects];
}
這里要用到一個MAS_VIEW(其實就是UIView)的一個分類句携。
@interface MAS_VIEW (MASConstraints)
@property (nonatomic, readonly) NSMutableSet *mas_installedConstraints;
@end
@implementation MAS_VIEW (MASConstraints)
static char kInstalledConstraintsKey;
- (NSMutableSet *)mas_installedConstraints {
//運行時獲取當(dāng)前view的所有安裝的約束
NSMutableSet *constraints = objc_getAssociatedObject(self, &kInstalledConstraintsKey);
if (!constraints) {
constraints = [NSMutableSet set];
objc_setAssociatedObject(self, &kInstalledConstraintsKey, constraints, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return constraints;
}
@end
同MASCompositeConstraint
類一樣,在.m文件里面MASViewConstraint
也重寫了很多的父類的方法允乐。結(jié)構(gòu)很清晰矮嫉。
結(jié)構(gòu)
下面看一下該類的結(jié)構(gòu)
大家可以看見:
- 在.h中只暴露出來一個實例化方法和返回所有已安裝約束的方法。
- 在.m中重寫了很多父類的方法牍疏,這個在講述
MASCompositeConstraint
時候說過蠢笋,是類似的。
后記
本篇主要講述了MASViewConstraint類解析鳞陨,感興趣的給個贊或者關(guān)注~~~~