美文网首页
Masonry研究1 View+MASAddtions

Masonry研究1 View+MASAddtions

作者: 独孤流 | 来源:发表于2017-05-23 14:04 被阅读37次

    View+MASAddtions


    使用Masonry主要用到的是UIView+MASAddtions的分类,这里面最主要的几个类如下

    1. 常用的类
    • MASViewAttribute 保存某个属性的类
    • MASConstraintMaker 用于设置约束

    这里一个很有意思的知识点,就是并没有直接写成UIView的分类,因为Masonry同事适配iOS、OSX,这样定义一个宏就可以一份代码不用区分具体是什么环境了

    2、 MASUtilities.h 知识点1

    #if TARGET_OS_IPHONE || TARGET_OS_TV
    
        #import <UIKit/UIKit.h>
        #define MAS_VIEW UIView
        #define MAS_VIEW_CONTROLLER UIViewController
        #define MASEdgeInsets UIEdgeInsets
    
        typedef UILayoutPriority MASLayoutPriority;
        static const MASLayoutPriority MASLayoutPriorityRequired = UILayoutPriorityRequired;
        static const MASLayoutPriority MASLayoutPriorityDefaultHigh = UILayoutPriorityDefaultHigh;
        static const MASLayoutPriority MASLayoutPriorityDefaultMedium = 500;
        static const MASLayoutPriority MASLayoutPriorityDefaultLow = UILayoutPriorityDefaultLow;
        static const MASLayoutPriority MASLayoutPriorityFittingSizeLevel = UILayoutPriorityFittingSizeLevel;
    
    #elif TARGET_OS_MAC
    
        #import <AppKit/AppKit.h>
        #define MAS_VIEW NSView
        #define MASEdgeInsets NSEdgeInsets
    
        typedef NSLayoutPriority MASLayoutPriority;
        static const MASLayoutPriority MASLayoutPriorityRequired = NSLayoutPriorityRequired;
        static const MASLayoutPriority MASLayoutPriorityDefaultHigh = NSLayoutPriorityDefaultHigh;
        static const MASLayoutPriority MASLayoutPriorityDragThatCanResizeWindow = NSLayoutPriorityDragThatCanResizeWindow;
        static const MASLayoutPriority MASLayoutPriorityDefaultMedium = 501;
        static const MASLayoutPriority MASLayoutPriorityWindowSizeStayPut = NSLayoutPriorityWindowSizeStayPut;
        static const MASLayoutPriority MASLayoutPriorityDragThatCannotResizeWindow = NSLayoutPriorityDragThatCannotResizeWindow;
        static const MASLayoutPriority MASLayoutPriorityDefaultLow = NSLayoutPriorityDefaultLow;
        static const MASLayoutPriority MASLayoutPriorityFittingSizeCompression = NSLayoutPriorityFittingSizeCompression;
    
    #endif
    

    3、 获取布局属性,直接用MASViewAttribute得到

    #pragma mark - NSLayoutAttribute properties
    
    - (MASViewAttribute *)mas_left {
        return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeft];
    }
    

    4、获取两个类最近的共同父类,使用了一个2层递归遍历,思路是用第一个View,也就是当前View的每一个父类(包含第一个类自己)与第二个View的所有父类(包括第二个类自己)进行比较,这样只要有相等的则是共同的父类,复杂度为n*m
    不知道具体效率怎么样

    #pragma mark - heirachy
    
    - (instancetype)mas_closestCommonSuperview:(MAS_VIEW *)view {
        MAS_VIEW *closestCommonSuperview = nil;
    
        MAS_VIEW *secondViewSuperview = view;
        while (!closestCommonSuperview && secondViewSuperview) {
            MAS_VIEW *firstViewSuperview = self;
            while (!closestCommonSuperview && firstViewSuperview) {
                if (secondViewSuperview == firstViewSuperview) {
                    closestCommonSuperview = secondViewSuperview;
                }
                firstViewSuperview = firstViewSuperview.superview;
            }
            secondViewSuperview = secondViewSuperview.superview;
        }
        return closestCommonSuperview;
    }
    

    5、 添加约束的过程就是以下几个过程

    • 设置不响应Autoresizing
    • 创建一个MASConstraintMaker实例
    • 通过block把约束保存到MASConstraintMaker里
    • 然后调用MASConstraintMaker的install方法把约束加到View上并返回一个数组
    • 修改就是给maker添加一个updateExisting=YES,重置就是给removeExisting=YES,默认设置时这两个属性都是NO
    • 返回的数组是所有添加的MASContriaints
    - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block {
        self.translatesAutoresizingMaskIntoConstraints = NO;
        MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
        block(constraintMaker);
        return [constraintMaker install];
    }
    

    相关文章

      网友评论

          本文标题:Masonry研究1 View+MASAddtions

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