美文网首页
OC 链式编程

OC 链式编程

作者: 七月不下雨 | 来源:发表于2017-07-18 16:31 被阅读0次
赏心悦目
  • _ config.h _
@interface Config : NSObject
  
   +(instancetype)defaultConfig;

   @property (nonatomic, strong) UIColor  *vBGColor; //小驼峰命名规范
   @property (nonatomic, assign) CGFloat  width;

     /**背景颜色*/
   @property (nonatomic, copy, readonly) Config *(^vBackGroundColor)(UIColor *color);
      /**宽度*/
   @property (nonatomic, copy, readonly) Config *(^vWidth)(CGFloat width);

@end
@implementation Config
 
 +(instancetype)defaultConfig{
      Config *config = [[Config alloc] init];
      config.vBGColor = [UIColor clearColor];
      config.width = 30.0f;
      return config;
  }

  -(Config *(^)(UIColor *)) vBackGroundColor{
      return ^(UIColor *color){
        self.vBGColor = color;
        return self;
      }
    }

  -(Config *(^)(UIColor *)) vWidth{
      return ^(CGFloat width){
        self.width = width;
        return self;
      }
   }

@end
  • View
@interface MMSegmentBar : UIView

/**
 快速创建一个控件

 @param frame frame
 @return view
 */
+ (instancetype)viewWithFrame: (CGRect)frame;

- (void)updateWithConfig:(void(^)(Config *config))configBlock;

@end
@interface View ()

@property (nonatomic, strong) Config *config;

@end

@implementation MMSegmentBar

+(instancetype)viewWithFrame:(CGRect)frame{
    View *view = [[View alloc] initWithFrame:frame];
    return view;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = self.config.vBGColor;
    }
    return self;
}

-(void)updateWithConfig:(void (^)(Config *))configBlock{
    if (configBlock) {
        configBlock(self.config);
    }

    // 按照当前的self.config 进行刷新
    self.backgroundColor = self.config.vBGColor;


    [self setNeedsLayout];
    [self layoutIfNeeded];
}

#pragma mark - layout
- (void)layoutSubviews{
    [super layoutSubviews];

    // 更新布局

}
#pragma mark -- lazy
- (Config *)config{
    if (!_config) {
        _config = [Config defaultConfig];
    }
    return _config;
}

  • Example
 View * view = [View viewWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
 [view updateWithConfig:^(Config *config) {
        config.vBackGroundColor([UIColor whiteColor]).vWidth(20.0f);
  }];

既然已经决定,就勇敢的去吧。

决定吧

相关文章

  • 像swift一样书写OC代码OC链式编程实践

    链式编程特点 链式编程 = 点语法 事物 串联 同样的hello word代码 OC和swift调用函数时候最大的...

  • iOS 链式编程

    链式编程是OC中一种很好的设计模式。框架中使用链式编程,会让框架使用者感觉写的代码更加美观简洁。 链式编程的效果 ...

  • OC 链式编程

    _ config.h _ View Example 既然已经决定,就勇敢的去吧。 决定吧

  • OC:链式编程

    概念: 链式编程:将多个业务逻辑(方法)通过“.”(点号)串联起来的一种代码风格,形似链条,故称链式编程。核心思想...

  • iOS链式语法深入实践

    要点 什么是链式语法 OC中的RAC、Masonry、SnapKit等链式编程的典型,大家应该都熟悉了Masonr...

  • OC-链式编程

    原理:用block作为返回值实现的链式 优点通过点语法直接调用 缺点虽然点语法调出方法,但是()和括号内的值需要手...

  • 链式文件生成器原理分析(一)

    在OC里面实现链式编程,可以使用返回调用者自身来实现。但是类有很多,每个类也有很多方法,假如要实现链式编程,则需要...

  • oc链式赋值

    偶然发现同事写的代码里面有两个连续的赋值,甚是懵逼,查了下链式赋值要么是oc的链式编程要么是python的链式赋值...

  • jQuery 链式编程

    jQuery 链式编程 链式编程原理 链式编程代码示例 隐式迭代

  • 链式编程思想

    链式编程思想 链式编程思想

网友评论

      本文标题: OC 链式编程

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