分析Masonry

时间:2024-07-10 19:07:32

一. 继承关系

1.MASConstraint (abstract)

MASViewContraint    MASComposisionConstraint

2. UIView      NSLayoutConstraint

MAS_View     MASLayoutConstraint

3. MASViewConstrant MASViewAttribute

二. 包含关系

MASViewContraint继承与MASConstraint

拥有 两个MASViewAttribute *属性

@property (nonatomic, strong, readonly) MASViewAttribute *firstViewAttribute;

@property (nonatomic, strong, readonly) MASViewAttribute *secondViewAttribute;

MASViewContraint是整个Masonry框架的核心

拥有MASConstraint的所有属性和方法,并必须重写MASConstraint的抽象方法

具体有以下, 主要的功能是包装MASConstraint并设置进入MASConstraintMaker的constrains数组中,

- (MASConstraint *)left;

- (MASConstraint *)top;

- (MASConstraint *)right;

- (MASConstraint *)bottom;

- (MASConstraint *)leading;

- (MASConstraint *)trailing;

- (MASConstraint *)width;

- (MASConstraint *)height;

- (MASConstraint *)centerX;

- (MASConstraint *)centerY;

- (MASConstraint *)baseline;

#if TARGET_OS_IPHONE

- (MASConstraint *)leftMargin;

- (MASConstraint *)rightMargin;

- (MASConstraint *)topMargin;

- (MASConstraint *)bottomMargin;

- (MASConstraint *)leadingMargin;

- (MASConstraint *)trailingMargin;

- (MASConstraint *)centerXWithinMargins;

- (MASConstraint *)centerYWithinMargins;

#endif

每个MASViewAttribute拥有 一个MAX_View属性 和一个 NSLayoutAttribute属性

@property (nonatomic, weak, readonly) MAS_VIEW *view;

@property (nonatomic, assign, readonly) NSLayoutAttribute layoutAttribute;

MASConstraintMaker 作为MASViewContraintDelegate的委托接收者, 主要负责收集MASViewContraint进入constraints数组属性, 以及安装constraints数组内的约束

@property (nonatomic, weak) MAS_VIEW *view;

@property (nonatomic, strong) NSMutableArray *constraints;

定义必须要被重写否则会抛出异常方法

#define MASMethodNotImplemented() \
@throw [NSException exceptionWithName:NSInternalInconsistencyException \
reason:[NSString stringWithFormat:@"You must override %@ in a subclass.", NSStringFromSelector(_cmd)] \
userInfo:nil]

定义类似其他语言(如Java/C#)的方法调用

- (MASConstraint * (^)(NSValue *value))valueOffset {
return ^id(NSValue *offset) {
NSAssert([offset isKindOfClass:NSValue.class], @"expected an NSValue offset, got: %@", offset);
[self setLayoutConstantWithValue:offset];
return self;
};
}