美文网首页iOS源码探究相关iOS开发交流iOS精华
Masonry源码分析三:链式语法分析和一些其他细节

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

作者: daixunry | 来源:发表于2015-11-11 12:09 被阅读1567次

本章大致描述一下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的顶部了,底部之类的

相关文章

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

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

  • iOS框架·Masonry源码深度解析及学习启示:设计模式与链式

    传送门:链式编程小Demo 这篇文章是 Masonry 框架源码的解析和笔记。学习Masonry之前,先了解这个框...

  • RAC之masonry源码深度解析

    写在前面:本文不是讲解masonry的基础使用,而是希望借着masonry的源码解析给大家渗透链式编程的思想和展示...

  • Masonry源码分析与链式编程

    在ios开发中,Masonry是最常用的第三方开发布局框架。Masonry是基于自动布局技术实现的,所以说Maso...

  • iOS 链式组件库封装

    读完Masonry源码有段时间,Masonry对链式的封装堪称优秀。学习第三方库的第一境界是使用,第二境界是能有自...

  • Masonry源码链式函数

    在研究Masonry源代码之前,自己也尝试书写了一边简易版的Masonry,我写的简易版学习代码可以去参考我的gi...

  • iOS链式编程

    谈到链式编程和函数式编程, Masonry就是最经典的代表, 没事可以多看看它的源码。例如:make.top.eq...

  • iOS Masonry

    Masonry简介 Masonry利用简化,链式和富有表现力的语法,利用AutoLayout NSLayoutCo...

  • Masonry框架源码分析

    Masonry框架源码分析 相信大多数iOS开发者对Masonry框架并不陌生 , 本文是笔者通读Masonry的...

  • 链式语法学习

    为什么要写一个链式语法的库 最近看了 JHChainableAnimations 和 Masonry 觉得链式语法...

网友评论

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

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