在上篇我介紹了一下Masonry使用:http://www.lxweimin.com/p/894816db541c
我們會(huì)發(fā)現(xiàn)Masonry寫(xiě)法其實(shí)就是鏈?zhǔn)骄幊趟枷?我們可以點(diǎn)進(jìn)Masonry類(lèi)里面去看它的具體實(shí)現(xiàn)方法如下
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;
};
}
從上面我們不難看出鏈?zhǔn)骄幊痰姆椒ㄒ膊浑y寫(xiě),這些我們都可以應(yīng)用到我們平時(shí)寫(xiě)控件設(shè)置控件某些屬性的方法中去
下面我們以UIbutton為例寫(xiě)個(gè)例子
首先我們先新建一個(gè)UIbutton的分類(lèi)
然后我們可以給方法名前面加個(gè)"cp_"(Chain programming 鏈?zhǔn)骄幊?前綴區(qū)分它本來(lái)的方法
UIButton+PC.h文件
#import <UIKit/UIKit.h>
@interface UIButton (PC)
/**
鏈?zhǔn)骄幊讨O(shè)置title
*/
- (UIButton *(^)(NSString *title))cp_title;
/**
鏈?zhǔn)骄幊讨O(shè)置titleColor
*/
- (UIButton *(^)(UIColor *color))cp_titleColor;
/**
鏈?zhǔn)骄幊讨O(shè)置font
*/
- (UIButton *(^)(UIFont *font))cp_font;
/**
鏈?zhǔn)骄幊讨O(shè)置backgroundColor
*/
- (UIButton *(^)(UIColor *color))cp_backgroundColor;
/**
鏈?zhǔn)骄幊讨O(shè)置imageName
*/
- (UIButton *(^)(NSString *name))cp_imageName;
/**
鏈?zhǔn)骄幊讨O(shè)置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
然后在你需要使用的類(lèi)中#import "UIButton+PC.h" 寫(xiě)代碼的時(shí)候就會(huì)出現(xiàn)這些方法了
- (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(@"鏈?zhǔn)骄幊?).cp_font([UIFont systemFontOfSize:16]).cp_titleColor([UIColor blackColor]).cp_backgroundColor([UIColor yellowColor]).cp_action(self,@selector(clickAction_add));
}
然后我們?cè)僬f(shuō)說(shuō)這么寫(xiě)的優(yōu)點(diǎn)和缺點(diǎn)
如果不用鏈?zhǔn)骄幊虒?xiě)法,那么我們一般是封裝一個(gè)方法,然后多帶幾個(gè)參數(shù)來(lái)設(shè)置
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];
}
我們這樣寫(xiě)其實(shí)和鏈?zhǔn)骄幊虒?xiě)法效果一樣,但是有些時(shí)候我們某些屬性不需要設(shè)置,我們可能只需要設(shè)置一兩個(gè)屬性,這時(shí)候如果寫(xiě)兩行代碼又覺(jué)得有點(diǎn)多,調(diào)用這個(gè)public_setTitle方法寫(xiě)的更多有點(diǎn)雞肋,就顯得不夠靈活,這個(gè)時(shí)候鏈?zhǔn)骄幊痰膶?xiě)法就有優(yōu)勢(shì)了,你可以選擇設(shè)置自己想要改變的任何屬性而不用寫(xiě)多余代碼,這還是只是幾個(gè)屬性設(shè)置,如果把layer的一些屬性設(shè)置寫(xiě)上了就更多了,所以鏈?zhǔn)骄幊虒?duì)于對(duì)象有很多屬性設(shè)置時(shí)是很有優(yōu)點(diǎn)的.
然后鏈?zhǔn)骄幊虒?xiě)法也有缺點(diǎn),那就是寫(xiě)代碼時(shí)代碼衍生時(shí)不會(huì)把方法的參數(shù)顯示出來(lái),必須自己手動(dòng)去看需要啥參數(shù).不過(guò)這一般不太影響我們編程!
大家如何提高編程效率都有自己的一套熟悉的方法,這次分享希望大家喜歡!