假如当前iOS项目开发语言是OC, 那么block肯定使用过
下面是Masonry一个简单的使用:
UIView *testV = [[UIView alloc]init];
[self.view addSubview:testV];
[testV mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.left.right.equalTo(self.view);
}];
那么其中
1 make.top.bottom.left.right.
这种写法就是链式编程
,也就是通过点语法连接,像一条链子一样连接在一起.
2 XX.equalTo(self.view)
这种写法就是函数式编程
, 一般需要参数这个就是函数式.
1 一般使用UIButton的方法:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"我是按钮!" forState:UIControlStateNormal];
[button setImage:ImageName(placeholderimage) forState:UIControlStateNormal];
[button setBackgroundImage:ImageName(placeholderimage) forState:UIControlStateNormal];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
点击事件:
- (void)click:(UIButton *)sender {
DLog(@"点击了我!");
}
2 仿照Masonry我写一个UIButton的Category :
UIButton+Easy
(实现代码写在文章最后),最终实现上述功能效果:
UIButton *button = UIBtn().e_title(@"我是按钮!")
.e_imageBg(ImageName(placeholderimage))
.e_image(ImageName(placeholderimage))
.e_click(^{
DLog(@"点击了我!");
});
可以发现代码变得简单明了了.
优点:
上面简便的写法就是很实在的优点了!
缺点:
1 不会自动语法提示
对于通过点语法使用Xcode并没有自动语法提示, 那么需要自己添加了.
创建代码块create code snippet
:
比如.e_image 方法
这样使用.e_image时就能自动提示了,其他同类.
(有更好的办法请留言 !)
同时,进入文件位置: ~/资源库/Developer/Xcode/UserData/CodeSnippets
可以把对应的代码块拷贝到其他电脑上使用!
2 block 比较耗时
一般来说,封装的代码总是比基本语法耗时(代码量大了), OC再快也没有c快,更没有汇编快, 但是大众开发语言只会越来越高级, 因为设备性能越来越好了
通过for循环对上述代码分别循环100遍(一个500行左右的文件).
CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
for (int i = 0; i < 100; i++) {
xxx
xxx
xxx
}
CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
DLog(@"%@",[NSString stringWithFormat:@"block耗时:%0.6f", end - start]);
最终打印结果:
2019-03-06 10:04:40.8...
[Line 329] block耗时:0.994888
2019-03-06 10:04:40.8...
[Line 339] 常规耗时:0.001091
不用我解释了吧, 这耗时比很明显了!
当然实际项目中可能没有这么高的比例 , 但是当你大量运用时也不小啊 !
有人会想, 我几年工作经验的代码, 还没有刚出来的新手的代码跑得快, !
个人观点:
既要提高开发效率, 同时也要保证APP运行速度, 所以block的使用要量力而行, 不能太泛滥!
3 block 要注意崩溃 和 内存泄漏
这个应该都知道.
(1) 崩溃问题 :
//判断是否为nil
if (block) {
block(xx);
}
或者
!block ? : block(xx);
(2) 内存泄漏问题 :
内存泄漏
//内存泄漏:
btn.e_click(^{
[self.xx xx];
DLog(@"点击了我!");
});
解决
@weakify(self)
btn.e_click(^{
@strongify(self)
[self.xx xx];
DLog(@"点击了我!");
});
最后代码 供参考:
UIButton+Easy.h
@interface UIButton (Easy)
UIButton * UIBtn();
- (UIButton * (^)(NSString *title))e_title;
- (UIButton * (^)(UIImage *image))e_image;
- (UIButton * (^)(UIImage *imageBg))e_imageBg;
- (UIButton * (^)(UIColor *colorBg))e_colorBg;
- (UIButton * (^)(UIColor *colorT))e_colorT;
- (UIButton * (^)(NSInteger font))e_font;
- (UIButton * (^)(NSInteger fontB))e_fontB;
- (UIButton * (^)(NSInteger radius))e_radius;
- (UIButton * (^)(NSInteger borderWidth))e_borderWidth;
- (UIButton * (^)(UIColor *borderColor))e_borderColor;
- (UIButton * (^)(void (^)(void)))e_click;
@end
UIButton+Easy.m
#import "UIButton+Easy.h"
@implementation UIButton (Easy)
- (UIButton *(^)(NSString *))e_title {
return ^(NSString *text) {
[self setTitle:StrValue(text) forState:UIControlStateNormal];
return self;
};
}
- (UIButton * (^)(UIImage *image))e_image {
return ^(UIImage *image) {
[self setImage:image forState:UIControlStateNormal];
return self;
};
}
- (UIButton * (^)(UIImage *imageBg))e_imageBg {
return ^(UIImage *image) {
[self setBackgroundImage:image forState:UIControlStateNormal];
return self;
};
}
- (UIButton * (^)(UIColor *colorBg))e_colorBg {
return ^(UIColor *color) {
if (color) {
[self setBackgroundColor:color];
}
return self;
};
}
- (UIButton * (^)(UIColor *colorT))e_colorT {
return ^(UIColor *color) {
if (color) {
[self setTitleColor:color forState:UIControlStateNormal];
}
return self;
};
}
- (UIButton * (^)(NSInteger font))e_font {
return ^(NSInteger font) {
if (font >= 0) {
[self.titleLabel setFont:[UIFont systemFontOfSize:font]];
}
return self;
};
}
- (UIButton * (^)(NSInteger fontB))e_fontB {
return ^(NSInteger font) {
if (font >= 0) {
[self.titleLabel setFont:[UIFont boldSystemFontOfSize:font]];
}
return self;
};
}
- (UIButton * (^)(NSInteger radius))e_radius {
return ^(NSInteger radius) {
if (radius >= 0) {
self.layer.cornerRadius = radius;
self.layer.masksToBounds = YES;
}
return self;
};
}
- (UIButton * (^)(NSInteger borderWidth))e_borderWidth {
return ^(NSInteger borderWidth) {
if (borderWidth >= 0) {
if (borderWidth) {
self.layer.borderWidth = borderWidth;
}
}
return self;
};
}
- (UIButton * (^)(UIColor *borderColor))e_borderColor {
return ^(UIColor *borderColor) {
if (borderColor) {
if (borderColor) {
self.layer.borderColor = borderColor.CGColor;
}
}
return self;
};
}
/**
:点击事件
*/
- (UIButton * (^)(void (^)(void)))e_click {
UIButton *(^clickBlock)(void (^)(void)) = ^(void (^backBlock)(void)) {
[self bk_whenTapped:backBlock];
if (backBlock) {
backBlock();
}
return self;
};
return clickBlock;
}
@end
UIButton * UIBtn() {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
return button;
}