block 的一些理解

typedef  QGLabel *(^Block3)(UIColor *color);


@interface QGLabel : UILabel
@property (nonatomic, copy) Block3 block2;
@property (nonatomic, copy) Block3 block3;
@end


@implementation QGLabel
#pragma mark - 设置初始化数据
/** 设置数据 */
-(void)setupData{
    
    __weak typeof(self) weakSelf = self;
    self.block2 = ^QGLabel *(UIColor *color) {
        weakSelf.textColor = color;
        return weakSelf;
    };
}
    
-(Block3)block3{
    __weak typeof(self) weakSelf = self;
    return ^QGLabel *(UIColor *color) {
        weakSelf.backgroundColor = color;
        return weakSelf;
    };
}

@end



调用  调用了block3的 get 方法

label.block3([UIColor redColor]);


赋值   调用了block3的 set 方法

__weak typeof(self) weakSelf = self;
    self.block3 = ^QGLabel *(UIColor *color) {
        weakSelf.textColor = color;
        return weakSelf;
    };



说明:

 我们可以把 block 当做一个可以存储一段代码的属性

 如图的 block2 和 block3  我们就实现了链式编程

label.block3([UIColor yellowColor]).block2([UIColor redColor]);

猜你喜欢

转载自blog.csdn.net/qq_27074387/article/details/78284875