1-1编程思想Masonry链式编程

Autoresizing和Autolayout

Masonry框架&&链式编程

案例

手动导入Masonry或者cocopoods导入Masonry三方框架

#import "ViewController.h"
#import "Masonry.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //创建一个View
    UIView * redView = [[UIView alloc]init];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
    //链式编程思想特点:方法返回值必须要有方法调用者!!
    
    //添加约束  --  make约束制造者!!
    [redView mas_makeConstraints:^(MASConstraintMaker *make) {
        //设置约束 每次调用left\top就是将约束添加到数组中!
        /*
         MASConstraint * (^block)(id)  = make.left.top.equalTo;
         MASConstraint * mk = block(@10);
         mk.top;
        */
        make.left.top.equalTo(@10);//链式编程思想特点:方法返回值必须要有方法调用者!!   left和top都是直接返回调用者,equalTo是返回有方法调用者的block
        
        make.right.bottom.equalTo(@-10);
        /**
            mas_makeConstraints执行流程:
            1.创建约束制造者MASConstraintMaker,并且绑定控件,生成一个保存所有约束的数组
            2.执行mas_makeConstraints传入的Block
            3.让约束制造者安装约束!
                * 1.清空之前的所有约束
                * 2.遍历约束数组,一个一个安装
         
         */
        
    }];
    
    
}

使用Masonry框架进行链式编程,进而进行多个数相加相乘相除等等。

这里是以多数相乘为例

#import "ViewController.h"
#import "NSObject+sum.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    int result = [NSString hk_makeConstraints:^(sumManager *mgr) {
        mgr.add(10).add(20);

    }];
    NSLog(@"%d",result);
    
}
#import 
#import "sumManager.h"

@interface NSObject (sum)


+ (int )hk_makeConstraints:(void(^)(sumManager * mgr))block;
@end
#import "NSObject+sum.h"

@implementation NSObject (sum)
+(int)hk_makeConstraints:(void (^)(sumManager *))block
{
    sumManager * mgr = [[sumManager alloc]init];
    block(mgr);
    return mgr.result;
    
}
@end
#import 

@interface sumManager : NSObject
/**  */
@property(assign,nonatomic)int result;

//-(instancetype)add:(int)value;
-(sumManager *(^)(int value))add;

@end
#import "sumManager.h"

@implementation sumManager

-(sumManager * (^)(int value))add{
    return ^(int value){
        _result += value;
        return self;
    };
}

@end

你可能感兴趣的:(1-1编程思想Masonry链式编程)