美文网首页
从Masonry看链式编程

从Masonry看链式编程

作者: 小冰山口 | 来源:发表于2017-04-20 13:09 被阅读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];
}
  • 首先将translatesAutoresizingMaskIntoConstraints这个属性改成NO, 首先, 苹果官方是这么说的:

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已经帮你修改了这个属性.

  • 创建一个maker对象, 丢到block里面去, 供外界使用. 所以, 大家用的make..., 就是在这个时候创建的.

    • 创建的时候, 将调用者view传给make的属性view
    • 同时, 创建一个可变数组, 用来保存所有的约束对象
- (id)initWithView:(MAS_VIEW *)view {
    self = [super init];
    if (!self) return nil;
    
    self.view = view;
    self.constraints = NSMutableArray.new;
    
    return self;
}
  • 执行block

  • 安装约束

- (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));

这段代码里发生了什么?

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

  • 点语法的本质其实是get方法, (在这里可以这么理解!)
`MASConstraint`的属性 `MASConstraint`的`get`方法
  • 在这个里面, 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)
  • 这句代码之后, 还可以点语法点下去吗? 我们来看看equalTo是个什么东东:
- (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.

  • 然后提供两个get方法
- (NSObject * (^)(int number))minus;

- (NSObject * (^)(int number))plus;
- (int)yf_add:(void(^)(YFSumTool *tool))addBlock {
    YFSumTool *tool = [[YFSumTool alloc] init];
    addBlock(tool);
    return sum;
}
  • 在这个方法中, 先创建一个tool对象作为参数传到block里面去供外界使用, 然后, 调用block, 最后返回结果.

get方法实现如下:

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

- (NSObject * (^)(int number))minus {
    return ^(int number) {
        sum -= number;
        return self;
    };
}
  • get方法的返回值是block, 这个block有一个int类型的参数, 返回值为NSObject类型, 当这个block被调用时, 将你需要加或者减的数字作为参数传进去, 并将自身self返回出来, 这样下次可以继续调用, 点语法可以一直用下去:
链式编程

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

相关文章

  • 从Masonry看链式编程

    本人有若干成套学习视频, 可试看! 可试看! 可试看, 重要的事情说三遍 包含Java, 数据结构与算法, iOS...

  • 链式编程

    最近有时间看了下Masonry的实现原理,大家都知道Masonry就是经典的链式编程思想 链式编程小练习 1.创建...

  • 简单的链式编程(iOS)

    1.链式编程思想 最初接触Masonry框架的时候还不知道什么链式编程,最初的感觉就是Masonry使用起来很简洁...

  • 一、链式编程思想、响应式编程思想和函数式编程

    1. 链式编程思想 链式编程思想的核心就是方法的返回值必须是Block,Masonry是链式编程思想应用的代表。 ...

  • 编程思想-链式编程

    编程思想-链式编程 链式编程(链式调用)的主要表现是支持点语法结构的代码,代表库可以参照 Masonry 或 Sn...

  • 关于链式编程的理解:常看牢记

    模拟Masonry的链式编程,形式如: 思想(简单理解):

  • iOS 链式编程

    之前看到 Masonry 是使用链式编程,感觉很酷,自己简单的写了一个链式编程的示例! 首先简单的来说一下链式编程...

  • iOS链式编程及函数式编程

    提到链式编程和函数式编程,最典型的代表是Masonry 比较完美的实现了函数式编程和链式编程。例如 ``` [vi...

  • 链式编程

    最近接触了RAC,自然也就接触了链式编程,链式编程挺有意思的,代码让人感觉很简洁,masonry中就用到了链式编程...

  • Masonry 链式编程

    链式编程思想特点:方法返回值必须要有方法调用者!! 一、Masonry 使用 二、mas_makeConstrai...

网友评论

      本文标题:从Masonry看链式编程

      本文链接:https://www.haomeiwen.com/subject/csbkzttx.html