美文网首页
Masonry研究4 MASConstraint

Masonry研究4 MASConstraint

作者: 独孤流 | 来源:发表于2017-05-23 15:54 被阅读35次

    MASConstraint


    本类有两个子类
    1、MASViewConstraint,由一个单独的MASViewAttribute创建
    2、MASCompositeConstraint 由多个MASViewAttribute创建而成

    知识点,支持自动封装的一种写法

    /**
     *  Convenience auto-boxing macros for MASConstraint methods.
     *
     *  Defining MAS_SHORTHAND_GLOBALS will turn on auto-boxing for default syntax.
     *  A potential drawback of this is that the unprefixed macros will appear in global scope.
     */
    #define MASBoxValue(value) _MASBoxValue(@encode(__typeof__((value))), (value))
    #define mas_equalTo(...)                 equalTo(MASBoxValue((__VA_ARGS__)))
    #define mas_greaterThanOrEqualTo(...)    greaterThanOrEqualTo(MASBoxValue((__VA_ARGS__)))
    #define mas_lessThanOrEqualTo(...)       lessThanOrEqualTo(MASBoxValue((__VA_ARGS__)))
    
    #define mas_offset(...)                  valueOffset(MASBoxValue((__VA_ARGS__)))
    
    
    #ifdef MAS_SHORTHAND_GLOBALS
    
    #define equalTo(...)                     mas_equalTo(__VA_ARGS__)
    #define greaterThanOrEqualTo(...)        mas_greaterThanOrEqualTo(__VA_ARGS__)
    #define lessThanOrEqualTo(...)           mas_lessThanOrEqualTo(__VA_ARGS__)
    
    #define offset(...)                      mas_offset(__VA_ARGS__)
    
    #endif
    
    
    @interface MASConstraint (AutoboxingSupport)
    
    /**
     *  Aliases to corresponding relation methods (for shorthand macros)
     *  Also needed to aid autocompletion
     */
    - (MASConstraint * (^)(id attr))mas_equalTo;
    - (MASConstraint * (^)(id attr))mas_greaterThanOrEqualTo;
    - (MASConstraint * (^)(id attr))mas_lessThanOrEqualTo;
    
    /**
     *  A dummy method to aid autocompletion
     */
    - (MASConstraint * (^)(id offset))mas_offset;
    
    @end
    
    /**
     *  Given a scalar or struct value, wraps it in NSValue
     *  Based on EXPObjectify: https://github.com/specta/expecta
     */
    static inline id _MASBoxValue(const char *type, ...) {
        va_list v;
        va_start(v, type);
        id obj = nil;
        if (strcmp(type, @encode(id)) == 0) {
            id actual = va_arg(v, id);
            obj = actual;
        } else if (strcmp(type, @encode(CGPoint)) == 0) {
            CGPoint actual = (CGPoint)va_arg(v, CGPoint);
            obj = [NSValue value:&actual withObjCType:type];
        } else if (strcmp(type, @encode(CGSize)) == 0) {
            CGSize actual = (CGSize)va_arg(v, CGSize);
            obj = [NSValue value:&actual withObjCType:type];
        } else if (strcmp(type, @encode(MASEdgeInsets)) == 0) {
            MASEdgeInsets actual = (MASEdgeInsets)va_arg(v, MASEdgeInsets);
            obj = [NSValue value:&actual withObjCType:type];
        } else if (strcmp(type, @encode(double)) == 0) {
            double actual = (double)va_arg(v, double);
            obj = [NSNumber numberWithDouble:actual];
        } else if (strcmp(type, @encode(float)) == 0) {
            float actual = (float)va_arg(v, double);
            obj = [NSNumber numberWithFloat:actual];
        } else if (strcmp(type, @encode(int)) == 0) {
            int actual = (int)va_arg(v, int);
            obj = [NSNumber numberWithInt:actual];
        } else if (strcmp(type, @encode(long)) == 0) {
            long actual = (long)va_arg(v, long);
            obj = [NSNumber numberWithLong:actual];
        } else if (strcmp(type, @encode(long long)) == 0) {
            long long actual = (long long)va_arg(v, long long);
            obj = [NSNumber numberWithLongLong:actual];
        } else if (strcmp(type, @encode(short)) == 0) {
            short actual = (short)va_arg(v, int);
            obj = [NSNumber numberWithShort:actual];
        } else if (strcmp(type, @encode(char)) == 0) {
            char actual = (char)va_arg(v, int);
            obj = [NSNumber numberWithChar:actual];
        } else if (strcmp(type, @encode(bool)) == 0) {
            bool actual = (bool)va_arg(v, int);
            obj = [NSNumber numberWithBool:actual];
        } else if (strcmp(type, @encode(unsigned char)) == 0) {
            unsigned char actual = (unsigned char)va_arg(v, unsigned int);
            obj = [NSNumber numberWithUnsignedChar:actual];
        } else if (strcmp(type, @encode(unsigned int)) == 0) {
            unsigned int actual = (unsigned int)va_arg(v, unsigned int);
            obj = [NSNumber numberWithUnsignedInt:actual];
        } else if (strcmp(type, @encode(unsigned long)) == 0) {
            unsigned long actual = (unsigned long)va_arg(v, unsigned long);
            obj = [NSNumber numberWithUnsignedLong:actual];
        } else if (strcmp(type, @encode(unsigned long long)) == 0) {
            unsigned long long actual = (unsigned long long)va_arg(v, unsigned long long);
            obj = [NSNumber numberWithUnsignedLongLong:actual];
        } else if (strcmp(type, @encode(unsigned short)) == 0) {
            unsigned short actual = (unsigned short)va_arg(v, unsigned int);
            obj = [NSNumber numberWithUnsignedShort:actual];
        }
        va_end(v);
        return obj;
    }
    
    #define MASBoxValue(value) _MASBoxValue(@encode(__typeof__((value))), (value))
    

    相关文章

      网友评论

          本文标题:Masonry研究4 MASConstraint

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