在上篇我介绍了一下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
@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" 写代码的时候就会出现这些方法了
- (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的一些属性设置写上了就更多了,所以链式编程对于对象有很多属性设置时是很有优点的.
然后链式编程写法也有缺点,那就是写代码时代码衍生时不会把方法的参数显示出来,必须自己手动去看需要啥参数.不过这一般不太影响我们编程!
大家如何提高编程效率都有自己的一套熟悉的方法,这次分享希望大家喜欢!