美文网首页
iOS链式编程

iOS链式编程

作者: 勤劳的小码 | 来源:发表于2018-09-21 11:32 被阅读0次

在上篇我介绍了一下Masonry使用:https://www.jianshu.com/p/894816db541c
我们会发现Masonry写法其实就是链式编程思想,我们可以点进Masonry类里面去看它的具体实现方法如下

MASConstraint.h文件

/**
 *  Modifies the NSLayoutConstraint constant,
 *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
 *  NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight
 */
- (MASConstraint * (^)(MASEdgeInsets insets))insets;

/**
 *  Modifies the NSLayoutConstraint constant,
 *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
 *  NSLayoutAttributeWidth, NSLayoutAttributeHeight
 */
- (MASConstraint * (^)(CGSize offset))sizeOffset;

MASConstraint.m文件

- (MASConstraint * (^)(MASEdgeInsets))insets {
    return ^id(MASEdgeInsets insets){
        self.insets = insets;
        return self;
    };
}

- (MASConstraint * (^)(CGSize))sizeOffset {
    return ^id(CGSize offset) {
        self.sizeOffset = offset;
        return self;
    };
}

从上面我们不难看出链式编程的方法也不难写,这些我们都可以应用到我们平时写控件设置控件某些属性的方法中去
下面我们以UIbutton为例写个例子

首先我们先新建一个UIbutton的分类


新建分类第一步 新建分类第二步

然后我们可以给方法名前面加个"cp_"(Chain programming 链式编程)前缀区分它本来的方法

UIButton+PC.h文件

#import <UIKit/UIKit.h>

@interface UIButton (PC)

/**
 链式编程之设置title
 */
- (UIButton *(^)(NSString *title))cp_title;

/**
 链式编程之设置titleColor
 */
- (UIButton *(^)(UIColor *color))cp_titleColor;

/**
 链式编程之设置font
 */
- (UIButton *(^)(UIFont *font))cp_font;

/**
 链式编程之设置backgroundColor
 */
- (UIButton *(^)(UIColor *color))cp_backgroundColor;

/**
 链式编程之设置imageName
 */
- (UIButton *(^)(NSString *name))cp_imageName;

/**
 链式编程之设置action
 */
- (UIButton *(^)(id object, SEL action))cp_action;

@end

UIButton+PC.m文件

#import "UIButton+PC.h"

@implementation UIButton (PC)

- (UIButton *(^)(NSString *title))cp_title {
    return ^(NSString *title) {
        [self setTitle:title forState:UIControlStateNormal];
        return self;
    };
}

- (UIButton *(^)(UIColor *color))cp_titleColor {
    return ^(UIColor *color) {
        [self setTitleColor:color forState:UIControlStateNormal];
        return self;
    };
}

- (UIButton *(^)(UIFont *font))cp_font {
    return ^(UIFont *font) {
        self.titleLabel.font = font;
        return self;
    };
}

- (UIButton *(^)(UIColor *color))cp_backgroundColor {
    return ^(UIColor *color) {
        self.backgroundColor = color;
        return self;
    };
}

- (UIButton *(^)(NSString *name))cp_imageName {
    return ^(NSString *name) {
        [self setBackgroundImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
        return self;
    };
}

- (UIButton *(^)(id object, SEL action))cp_action {
    return ^(id object, SEL action) {
        [self addTarget:object action:action forControlEvents:UIControlEventTouchUpInside];
        return self;
    };
}

@end

然后在你需要使用的类中#import "UIButton+PC.h" 写代码的时候就会出现这些方法了


链式编程UIbutton例子
- (void)xn_initCPSubViews {
    UIButton *cpButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:cpButton];
    [cpButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(200, 50));
    }];
    cpButton.cp_title(@"链式编程").cp_font([UIFont systemFontOfSize:16]).cp_titleColor([UIColor blackColor]).cp_backgroundColor([UIColor yellowColor]).cp_action(self,@selector(clickAction_add));
}
效果图

然后我们再说说这么写的优点和缺点
如果不用链式编程写法,那么我们一般是封装一个方法,然后多带几个参数来设置

UIButton+PC.h文件

- (void)public_setTitle:(NSString *)title
                   font:(UIFont *)font
             titleColor:(UIColor *)titleColor
        backgroundColor:(UIColor *)backgroundColor
                 target:(id)target
                 action:(SEL)action;

UIButton+PC.m文件

- (void)public_setTitle:(NSString *)title
                   font:(UIFont *)font
             titleColor:(UIColor *)titleColor
        backgroundColor:(UIColor *)backgroundColor
                 target:(id)target
                 action:(SEL)action {
    [self setTitle:title forState:UIControlStateNormal];
    [self setTitleColor:titleColor forState:UIControlStateNormal];
    self.titleLabel.font = font;
    self.backgroundColor = backgroundColor;
    [self addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}

我们这样写其实和链式编程写法效果一样,但是有些时候我们某些属性不需要设置,我们可能只需要设置一两个属性,这时候如果写两行代码又觉得有点多,调用这个public_setTitle方法写的更多有点鸡肋,就显得不够灵活,这个时候链式编程的写法就有优势了,你可以选择设置自己想要改变的任何属性而不用写多余代码,这还是只是几个属性设置,如果把layer的一些属性设置写上了就更多了,所以链式编程对于对象有很多属性设置时是很有优点的.

然后链式编程写法也有缺点,那就是写代码时代码衍生时不会把方法的参数显示出来,必须自己手动去看需要啥参数.不过这一般不太影响我们编程!

大家如何提高编程效率都有自己的一套熟悉的方法,这次分享希望大家喜欢!

相关文章

  • 工作iOS技术总结

    链式编程、函数式、面向接口编程思想 iOS 之ReactiveCocoa 链式编程2 WKWebView的缓存处理...

  • ReactiveObjC入门

    ReactiveObjC基础用法 iOS开发三种编程方式(响应式编程、函数编程、链式编程),函数编程最常用,链式编...

  • 链式编程总结

    链式编程总结 @(iOS) 研究了一下链式编程,但是感觉项目中用处不是很多。 介绍 1.什么时候使用链式编程?在面...

  • iOS 链式编程简单的使用

    iOS 链式编程简单的使用 链式编程-顾名思义,链式,连贯性为其主要特征,放在编程领域来讲,说简单点就是把一系列的...

  • iOS-链式编程思想

    在iOS中,链式编程虽然用的不太多,但是,在特定的应用环境下,利用block实现链式编程的话,会大大的提高编程效率...

  • iOS链式编程

    在iOS中,链式编程虽然用的不太多,但是,在特定的应用环境下,利用block实现链式编程的话,会大大的提高编程效率...

  • iOS链式、函数式和响应式编程

    原文编程思想-iOS链式、函数式和响应式编程 在了解链式、函数式和响应式编程前,我们需要回顾下Block,它在下面...

  • iOS 链式编程

    本来想将题目取名为 masonry源码心得之链式编程 的。但想着想着没什么必要,我写文章主要目的是做笔记自己看的。...

  • iOS链式编程

    在上篇我介绍了一下Masonry使用:https://www.jianshu.com/p/894816db541c...

  • iOS链式编程

    谈到链式编程和函数式编程, Masonry就是最经典的代表, 没事可以多看看它的源码。例如:make.top.eq...

网友评论

      本文标题:iOS链式编程

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