iOS源码探究相关iOS开发交流iOS精华

Masonry源码分析三:链式语法分析和一些其他细节

2015-11-11  本文已影响1567人  daixunry

本章大致描述一下Masonry是如何支持链式语法的,并且对框架的其他一些文件做一些描述

第一部分 了解一下链式语法是如何实现的###

实际上前面两篇文章已经讲述的差不多了,如果看过源码,应该就会很轻松的知道,Masonry是如何支持链式语法的结构。

还是拿一个例子的讲解一下吧,(下方代码已删除不甚相关的代码)

    make.top.left究竟发生了什么?
    
    我们知道make.top返回的是MASConstraint对象,更确切的说是MASViewConstraint对象,
    上篇文章中我们知道,对MASConstraint调用top、left等,会返回一个MASCompositeConstraint对象;
    这个对象,会包含一个数组,在这个例子中存放的就是layoutAttribute分别为top和left的
    两个MASViewConstraint对象。
    
    1、MASConstraint的left方法:
    
    - (MASConstraint *)left {
        return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeft];
    }
    它会跳转到MASViewConstraint的addConstraintWithLayout...方法
--------------------------------------------    
    2、MASViewConstraint的addConstraintWithLayout...方法

    - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
        return [self.delegate constraint:self addConstraintWithLayoutAttribute:layoutAttribute];
    }
    它会调用delegate的constraint...delegate就是MASConstraintMaker,来看看吧
--------------------------------------------
    3、MASConstraintMaker的代理方法
    
    - (MASConstraint *)constraint:(MASConstraint *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
            MASCompositeConstraint *compositeConstraint = [[MASCompositeConstraint alloc] initWithChildren:children];
            [self constraint:constraint shouldBeReplacedWithConstraint:compositeConstraint];
            return compositeConstraint;
    }
    

第二部分 一些Category###



  1、NSArray (MASAdditions)

array内部是view,遍历array,逐一对view调用这些方法

    - (NSArray *)mas_makeConstraints:(void (^)(MASConstraintMaker *make))block;
    - (NSArray *)mas_updateConstraints:(void (^)(MASConstraintMaker *make))block;
    - (NSArray *)mas_remakeConstraints:(void (^)(MASConstraintMaker *make))block;

2、View+MASAdditions

我们平时,也可以对view调用一些方法例如:

    @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;
    

可以看到,返回的是MASViewAttribute对象

剩下的就是比较简单的mas_makeConstraint...

3、ViewController+MASAdditions

    @property (nonatomic, strong, readonly) MASViewAttribute *mas_topLayoutGuide;
    @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomLayoutGuide;
    @property (nonatomic, strong, readonly) MASViewAttribute *mas_topLayoutGuideTop;
    @property (nonatomic, strong, readonly) MASViewAttribute *mas_topLayoutGuideBottom;
    @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomLayoutGuideTop;
    @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomLayoutGuideBottom;

这些就是对齐self.view的顶部导航栏和底步tabbar的顶部了,底部之类的

上一篇下一篇

猜你喜欢

热点阅读