美文网首页
举例浅淡Block链式编程

举例浅淡Block链式编程

作者: cyh老崔 | 来源:发表于2017-09-12 23:17 被阅读63次

    一. 简述

    在项目中, Block的链式编程肯定是会用到的. 如一些框架等.
    而对于项目中一些自定义的工具或view, 在以下情况时:

    • 1.可以包装完整
    • 2.有多个属性可以设置
    • 3.在项目中多处被用到

    最好实现Block的链式编程, 如此对团队非常有好处.
    注: block的链式, 即block有返回值, 且返回当前对象

    二.举例

    项目中需要自定义UINavigationBar, 符合上述3个条件.

    • 1.设置只读的, 有返回值的block属性
    /** 设置下划线 */
    @property (nonatomic, copy, readonly) YHNavigationBarView *(^setupUnderline)(void);
    
    ///左按钮图片
    @property (nonatomic, copy, readonly) YHNavigationBarView *(^leftBtnImage)(UIImage *image);
    ///左按钮标题
    @property (nonatomic, copy, readonly) YHNavigationBarView *(^leftBtnTitle)(NSString *title);
    ///右按钮图片
    @property (nonatomic, copy, readonly) YHNavigationBarView *(^rightBtnImage)(UIImage *image);
    ///右按钮标题
    @property (nonatomic, copy, readonly) YHNavigationBarView *(^rightBtnTitle)(NSString *title);
    ///标题按钮图片
    @property (nonatomic, copy, readonly) YHNavigationBarView *(^titleBtnImage)(UIImage *image);
    ///标题按钮的标题
    @property (nonatomic, copy, readonly) YHNavigationBarView *(^titleBtnTitle)(NSString *title);
    
    • 2.重写getter
    • 2.1
    ///设置下划线
    - (YHNavigationBarView *(^)(void))setupUnderline{
        return ^YHNavigationBarView *(void){
            
            //下划线
            UIView *underlineView = [UIView new];
            underlineView.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
            [self addSubview:underlineView];
            [underlineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self);
                make.right.equalTo(self);
                make.bottom.equalTo(self);
                make.height.equalTo(@1);
            }];
            
            return self;
        };
    }
    
    • 2.2
    ///左按钮图片
    - (YHNavigationBarView *(^)(UIImage *))leftBtnImage{
        return ^YHNavigationBarView *(UIImage *image){
            
            [self.leftBtn setImage:image forState:UIControlStateNormal];
            return self;
        };
    }
    

    其它getter方法与以上两个方法类似, 就不浪费篇章了.

    • 3.调用
    • 3.1 eg1
    self.navBar.setupLeftBackBtn(self, YHBackImgTypeBlack).setupUnderline().
        setupRightPlayBtn(self, nil, navBarplayBtn, ^{
            
            [UIViewController yh_fromCurrentShowingVcToDestionVc:[YHPlayMusicVc new]];
        });
    
    • 3.2 eg2
    self.navBar.setupLeftBackBtn(self, YHBackImgTypeBlack).
        setupTitleBtn(nil, @"修改昵称", nil);
    
    • 3.3 注意循环引用
    navBar.titleBtnAlpha(0).setupLeftBtn([UIImage imageNamed:@"nav_toolbar_back_white"], nil, ^{
            
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
        }).setupRightBtn([UIImage imageNamed:@"nav_share_white"], nil, ^{
            
            [weakSelf navRightButtonClick];
            
        }).setupTitleBtn(nil, self.titleStr, nil);
    

    相关文章

      网友评论

          本文标题:举例浅淡Block链式编程

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