从Masonry看链式编程

2017-04-20  本文已影响0人  小冰山口

本人有若干成套学习视频, 可试看! 可试看! 可试看, 重要的事情说三遍 包含Java, 数据结构与算法, iOS, 安卓, python, flutter等等, 如有需要, 联系微信tsaievan.

Masonry想必大家都用过, 今天带大家来稍微看一下内部实现的代码:

首先我写这么一段代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *yfView = [[UIView alloc] init];
    yfView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:yfView];
    
    [yfView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(@50);
        make.bottom.right.equalTo(@(-50));
    }];
}

实现这样一个效果, so easy对不对?

Masonry实现最简单效果
那么mas_makeConstraints:这个方法内部做了些什么呢?
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block {
    self.translatesAutoresizingMaskIntoConstraints = NO;
    MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
    block(constraintMaker);
    return [constraintMaker install];
}

By default, the autoresizing mask on a view gives rise to constraints that fully determine
the view's position. This allows the auto layout system to track the frames of views whose
layout is controlled manually (through -setFrame:, for example).
When you elect to position the view using auto layout by adding your own constraints,
you must set this property to NO. IB will do this for you.

如果你要使用自动布局, 这个属性就要改成NO, stroryBoard已经帮你修改了这个属性.

- (id)initWithView:(MAS_VIEW *)view {
    self = [super init];
    if (!self) return nil;
    
    self.view = view;
    self.constraints = NSMutableArray.new;
    
    return self;
}
- (void)install {
    if (self.hasBeenInstalled) {
        return;
    }
    
    if ([self supportsActiveProperty] && self.layoutConstraint) {
        self.layoutConstraint.active = YES;
        [self.firstViewAttribute.view.mas_installedConstraints addObject:self];
        return;
    }
    
    MAS_VIEW *firstLayoutItem = self.firstViewAttribute.item;
    NSLayoutAttribute firstLayoutAttribute = self.firstViewAttribute.layoutAttribute;
    MAS_VIEW *secondLayoutItem = self.secondViewAttribute.item;
    NSLayoutAttribute secondLayoutAttribute = self.secondViewAttribute.layoutAttribute;

    // alignment attributes must have a secondViewAttribute
    // therefore we assume that is refering to superview
    // eg make.left.equalTo(@10)
    if (!self.firstViewAttribute.isSizeAttribute && !self.secondViewAttribute) {
        secondLayoutItem = self.firstViewAttribute.view.superview;
        secondLayoutAttribute = firstLayoutAttribute;
    }
    
    MASLayoutConstraint *layoutConstraint
        = [MASLayoutConstraint constraintWithItem:firstLayoutItem
                                        attribute:firstLayoutAttribute
                                        relatedBy:self.layoutRelation
                                           toItem:secondLayoutItem
                                        attribute:secondLayoutAttribute
                                       multiplier:self.layoutMultiplier
                                         constant:self.layoutConstant];
    
    layoutConstraint.priority = self.layoutPriority;
    layoutConstraint.mas_key = self.mas_key;
    
    if (self.secondViewAttribute.view) {
        MAS_VIEW *closestCommonSuperview = [self.firstViewAttribute.view mas_closestCommonSuperview:self.secondViewAttribute.view];
        NSAssert(closestCommonSuperview,
                 @"couldn't find a common superview for %@ and %@",
                 self.firstViewAttribute.view, self.secondViewAttribute.view);
        self.installedView = closestCommonSuperview;
    } else if (self.firstViewAttribute.isSizeAttribute) {
        self.installedView = self.firstViewAttribute.view;
    } else {
        self.installedView = self.firstViewAttribute.view.superview;
    }


    MASLayoutConstraint *existingConstraint = nil;
    if (self.updateExisting) {
        existingConstraint = [self layoutConstraintSimilarTo:layoutConstraint];
    }
    if (existingConstraint) {
        // just update the constant
        existingConstraint.constant = layoutConstraint.constant;
        self.layoutConstraint = existingConstraint;
    } else {
        [self.installedView addConstraint:layoutConstraint];
        self.layoutConstraint = layoutConstraint;
        [firstLayoutItem.mas_installedConstraints addObject:self];
    }
}

上面这一大坨恶心的代码框架已经帮你写好了, 所以我们在外界调用的时候非常的方便.

但这不是今天链式编程的重点, 我们需要看的是:
make.top.left.equalTo(@50);
make.bottom.right.equalTo(@(-50));

这段代码里发生了什么?

这段代码里频繁使用了点语法

`MASConstraint`的属性 `MASConstraint`的`get`方法
- (MASConstraint *)constraint:(MASConstraint *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
    MASViewAttribute *viewAttribute = [[MASViewAttribute alloc] initWithView:self.view layoutAttribute:layoutAttribute];
    MASViewConstraint *newConstraint = [[MASViewConstraint alloc] initWithFirstViewAttribute:viewAttribute];
    if ([constraint isKindOfClass:MASViewConstraint.class]) {
        //replace with composite constraint
        NSArray *children = @[constraint, newConstraint];
        MASCompositeConstraint *compositeConstraint = [[MASCompositeConstraint alloc] initWithChildren:children];
        compositeConstraint.delegate = self;
        [self constraint:constraint shouldBeReplacedWithConstraint:compositeConstraint];
        return compositeConstraint;
    }
    if (!constraint) {
        newConstraint.delegate = self;
        [self.constraints addObject:newConstraint];
    }
    return newConstraint;
}

这样, 因为get方法是有返回值的, 返回值又是自身, 所以可以一直点下去.

话说到这里, 有一个疑问:

make.top.left.equalTo(@50)
- (MASConstraint * (^)(id))equalTo {
    return ^id(id attribute) {
        return self.equalToWithRelation(attribute, NSLayoutRelationEqual);
    };
}

首先, equalTo依然是一个get方法, 这个get方法的返回值是一个block, 这个方法的内部不仅仅设置约束, 还有一个返回值,返回值block直接在外界加一个括号()调用了, 调用之后, 这个block是一个参数为id类型, 返回值为MASConstraint *类型的, 这样, 在调用block之后, 又得到一个MASConstraint *对象, 就又可以使用点语法, 一直点下去.

equalTo方法中, 返回一大段block

- (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation {
    return ^id(id attribute, NSLayoutRelation relation) {
        if ([attribute isKindOfClass:NSArray.class]) {
            NSAssert(!self.hasLayoutRelation, @"Redefinition of constraint relation");
            NSMutableArray *children = NSMutableArray.new;
            for (id attr in attribute) {
                MASViewConstraint *viewConstraint = [self copy];
                viewConstraint.layoutRelation = relation;
                viewConstraint.secondViewAttribute = attr;
                [children addObject:viewConstraint];
            }
            MASCompositeConstraint *compositeConstraint = [[MASCompositeConstraint alloc] initWithChildren:children];
            compositeConstraint.delegate = self.delegate;
            [self.delegate constraint:self shouldBeReplacedWithConstraint:compositeConstraint];
            return compositeConstraint;
        } else {
            NSAssert(!self.hasLayoutRelation || self.layoutRelation == relation && [attribute isKindOfClass:NSValue.class], @"Redefinition of constraint relation");
            self.layoutRelation = relation;
            self.secondViewAttribute = attribute;
            return self;
        }
    };
}
下面,我们利用这种链式编程思想, 自己写一个加法小工具, 任何对象都可以利用这个小工具进行计算, 代码很简单, 关键是体会一下这种编程思想:
@interface NSObject (Add) 

- (int)yf_add:(void(^)(YFSumTool *tool))addBlock;

这里面我提供了一个tool, 相当于Masonry中的make.

- (NSObject * (^)(int number))minus;

- (NSObject * (^)(int number))plus;
- (int)yf_add:(void(^)(YFSumTool *tool))addBlock {
    YFSumTool *tool = [[YFSumTool alloc] init];
    addBlock(tool);
    return sum;
}

get方法实现如下:

- (NSObject * (^)(int number))plus {
    return ^(int number) {
        sum += number;
        return self;
    };
}

- (NSObject * (^)(int number))minus {
    return ^(int number) {
        sum -= number;
        return self;
    };
}
链式编程

PS. 本人有若干成套学习视频, 包含Java, 数据结构与算法, iOS, 安卓, python, flutter等等, 如有需要, 联系微信tsaievan.

上一篇 下一篇

猜你喜欢

热点阅读