1.链式
1.1链式使用:优势不言而喻。
make.left.equalTo(self).with.equalTo(self).height.mas_equalTo(50);
.语法实际上是函数的隐士调用。make.left等价于[make left],但如果有参数,.语法就无法连续使用了,我们可以巧用block当做返回值。
1.2带着以下几个问题去思考链式
1.2.1如果没有参数?返回对象本身吗?
返回的是对象本身,.left返回的是MASConstraint *
1.2.2.如果有参数,返回的是什么?
返回的是对象的Block,.equalTo返回的是MASConstraint * (^)(id)。而.equalTo()相当于是Block的调用。
1.2.3.而返回的对象Block又是如何能继续链式的呢?
把对象当做Block的返回值
- (MASConstraint * (^)(void))priorityHigh {
return ^id{
self.priority(MASLayoutPriorityDefaultHigh);
return self;
};
}
2.链式实现UILabel创建、属性设置。
链式效果
2.1 代码调用
ChainedLabel * label = [[ChainedLabel alloc] initWithFrame:CGRectMake(100, 100, 200, 100)];
label.text1(@"链式lable").textColor1([UIColor grayColor]).bgColor1([UIColor redColor]);
[self.view addSubview:label];
2.2 ChainedLabel实现
#import <UIKit/UIKit.h>
@class ChainedLabel;
typedef ChainedLabel *(^ChainedLabelBlock) (id);
NS_ASSUME_NONNULL_BEGIN
@interface ChainedLabel : UILabel
- (ChainedLabelBlock)bgColor1;
- (ChainedLabelBlock)textColor1;
- (ChainedLabelBlock)text1;
@end
NS_ASSUME_NONNULL_END
#import "ChainedLabel.h"
@implementation ChainedLabel
- (ChainedLabelBlock)text1{
ChainedLabelBlock block = ^ChainedLabel *(id text){
self.text = text;
return self;
};
return block;
}
- (ChainedLabelBlock)textColor1{
ChainedLabelBlock block = ^ChainedLabel *(id color){
self.textColor = color;
return self;
};
return block;
}
- (ChainedLabelBlock)bgColor1{
ChainedLabelBlock block = ^ChainedLabel *(id color){
self.backgroundColor = (UIColor *)color;
return self;
};
return block;
}
- (void)dealloc{
NSLog(@"ChainedLabel dealloc");
}
@end
2.3 实现分析
text1的参数实际上是返回的block进行传递的,而block内部进行属性设置操作。在Block返回return self,实际上是为了调用后再能继续进行链式。
实际调用中,我们可能并不希望直接使用ChainedLabel,而是使用系统的UILabel。直接把方法移到UILabel+Chained中即可。
3 重写之后的链式
3.1 代码调用
UILabel * label = UILabel
.createLabel
.frameEqualTo(CGRectMake(100, 100, 200, 100))
.textEqualTo(@"链式lable")
.textColorEqualTo([UIColor grayColor])
.bgColorEqualTo([UIColor redColor]);
[self.view addSubview:label];
3.2 UILabel+Chained实现
#import <UIKit/UIKit.h>
typedef UILabel *(^ChainedLabelBlock) (id);
typedef UILabel *(^ChainedLabelFrameBlock) (CGRect frame);
NS_ASSUME_NONNULL_BEGIN
@interface UILabel (Chained)
+ (UILabel *)createLabel;
- (ChainedLabelFrameBlock)frameEqualTo;
- (ChainedLabelBlock)bgColorEqualTo;
- (ChainedLabelBlock)textColorEqualTo;
- (ChainedLabelBlock)textEqualTo;
@end
NS_ASSUME_NONNULL_END
#import "UILabel+Chained.h"
@implementation UILabel (Chained)
+ (UILabel *)createLabel{
return [[UILabel alloc] init];
}
- (ChainedLabelFrameBlock)frameEqualTo{
ChainedLabelFrameBlock block = ^UILabel *(CGRect frame){
self.frame = frame;
return self;
};
return block;
}
- (ChainedLabelBlock)textEqualTo{
ChainedLabelBlock block = ^UILabel *(id text){
self.text = text;
return self;
};
return block;
}
- (ChainedLabelBlock)textColorEqualTo{
ChainedLabelBlock block = ^UILabel *(id color){
self.textColor = color;
return self;
};
return block;
}
- (ChainedLabelBlock)bgColorEqualTo{
ChainedLabelBlock block = ^UILabel *(id color){
self.backgroundColor = (UIColor *)color;
return self;
};
return block;
}
- (void)dealloc{
NSLog(@"ChainedLabel dealloc");
}
@end
总结:
1.链式实现不难,关键还是理解其本质。
2.链式语法简洁明了,方便调用,文中只是抛出一个UILabel的例子,无论是多个网络请求,多个动画执行又或者是其他UI的封装都可以使用链式解决。
3.链式在iOS中应用广泛:如Masonry,PormiseKit等。
4.UILabel、UIButton、UIBarButtonItem、NSMutableAttributedString已经被封装成组件WPChained,原理可参考这篇文章iOS 链式组件库封装.








网友评论