每次自定义UIBarButtonItem和UIButton时,想要监听点击方法时,都需要再写一个方法,有没有感觉很烦~,能不能使用blcok回调呢?
看完这篇以后你会这样用
// 自定义导航栏左边按钮
self.navigationItem.leftBarButtonItem = [GKBlockedBarButtonItem blockedBarButtonItemWithTitle:@"返回" eventHandler:^{
[weakSelf.navigationController popViewControllerAnimated:YES];
}];
// 自定义按钮,特意选了个最长的~
CGRect rect = CGRectMake(100, 100, 100, 100);
__weak typeof(self) weakSelf = self;
GKBlockedButton *blockedBtn = [GKBlockedButton blockedButtonWithTapHandler:^{
NSLog(@"点击了按钮");
[weakSelf.navigationController pushViewController:[TestViewController new] animated:YES];
} frame:rect addToSuperview:self.view];
blockedBtn.backgroundColor = [UIColor redColor];
有没有感觉很便捷呢,那么请继续往下看~,方法名通俗易懂就不写注释了,见谅
首先关于UIBarButtonItem
的
// 声明文件提供常用的接口
@interface GKBlockedBarButtonItem : UIBarButtonItem
+ (instancetype)blockedBarButtonItemWithTitle:(NSString *)title eventHandler:(void(^)())eventHandler;
+ (instancetype)blockedBarButtonItemWithImage:(UIImage *)image eventHandler:(void (^)())eventHandler;
+ (instancetype)blockedBarButtonItemWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem eventHandler:(void (^)())eventHandler;
+ (instancetype)blockedBarButtonItemWithCustomView:(UIView *)customView eventHandler:(void (^)())eventHandler;
@end
实现文件
#import "GKBlockedBarButtonItem.h"
@interface GKBlockedBarButtonItem ()
@property (nonatomic, copy) void(^eventHandler)();
@property (nonatomic, strong) UITapGestureRecognizer *customViewTapGestureRecognizer;
@end
@implementation GKBlockedBarButtonItem
- (instancetype)initWithTitle:(NSString *)title {
self = [super initWithTitle:title style:UIBarButtonItemStylePlain target:nil action:nil];
[self setup];
return self;
}
- (instancetype)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem {
self = [super initWithBarButtonSystemItem:systemItem target:nil action:nil];
[self setup];
return self;
}
- (instancetype)initWithImage:(UIImage *)image {
self = [super initWithImage:image style:UIBarButtonItemStylePlain target:nil action:nil];
[self setup];
return self;
}
- (void)setup {
self.target = self;
self.action = @selector(tapped);
}
- (instancetype)initWithCustomView:(UIView *)customView eventHandler:(void(^)())eventHandler {
self = [super initWithCustomView:customView];
if (eventHandler) {
self.customViewTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(customViewTapGestureRecognizer:)];
[customView addGestureRecognizer:self.customViewTapGestureRecognizer];
}
self.eventHandler = eventHandler;
return self;
}
- (void)tapped {
if (self.eventHandler) {
self.eventHandler();
}
}
- (void)customViewTapGestureRecognizer:(UITapGestureRecognizer *)gr {
[self tapped];
}
+ (instancetype)blockedBarButtonItemWithTitle:(NSString *)title eventHandler:(void (^)())eventHandler {
GKBlockedBarButtonItem *tmp = [[GKBlockedBarButtonItem alloc] initWithTitle:title];
tmp.eventHandler = eventHandler;
return tmp;
}
+ (instancetype)blockedBarButtonItemWithImage:(UIImage *)image eventHandler:(void (^)())eventHandler {
GKBlockedBarButtonItem *tmp = [[GKBlockedBarButtonItem alloc] initWithImage:image];
tmp.eventHandler = eventHandler;
return tmp;
}
+ (instancetype)blockedBarButtonItemWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem eventHandler:(void (^)())eventHandler {
GKBlockedBarButtonItem *tmp = [[GKBlockedBarButtonItem alloc] initWithBarButtonSystemItem:systemItem];
tmp.eventHandler = eventHandler;
return tmp;
}
+ (instancetype)blockedBarButtonItemWithCustomView:(UIView *)customView {
return [[self class] blockedBarButtonItemWithCustomView:customView eventHandler:nil];
}
+ (instancetype)blockedBarButtonItemWithCustomView:(UIView *)customView eventHandler:(void (^)())eventHandler {
GKBlockedBarButtonItem *item = [[GKBlockedBarButtonItem alloc] initWithCustomView:customView eventHandler:eventHandler];
return item;
}
- (void)dealloc {
if (self.customView && _customViewTapGestureRecognizer) {
[self.customView removeGestureRecognizer:_customViewTapGestureRecognizer];
}
}
@end
再来关于UIButton
的
@interface GKBlockedButton : UIButton
/**
* 点击事件回调
*/
@property (nonatomic, copy) void(^tapHandler)();
/**
* 快速创建带block回调的按钮
*
* @param tapHandler 回调事件
*
*/
+ (instancetype)blockedButtonWithTapHandler:(void(^)())tapHandler;
/**
* 快速创建带block回调的按钮
*
* @param tapHandler 回调事件
* @param frame 位置信息
*/
+ (instancetype)blockedButtonWithTapHandler:(void(^)())tapHandler frame:(CGRect)frame;
/**
* 快速创建带block回调的按钮,并添加到父控件
*
* @param tapHandler 回调事件
* @param frame 位置信息
* @param superview 父控件
*/
+ (instancetype)blockedButtonWithTapHandler:(void(^)())tapHandler frame:(CGRect)frame addToSuperview:(UIView *)superview;
@end
实现文件如下
@implementation GKBlockedButton
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
[self setup];
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
[self setup];
}
- (void)setup {
[self addTarget:self action:@selector(tapped) forControlEvents:UIControlEventTouchUpInside];
}
- (void)tapped {
if (_tapHandler) {
_tapHandler();
}
}
+ (instancetype)blockedButtonWithTapHandler:(void(^)())tapHandler {
return [self blockedButtonWithTapHandler:tapHandler frame:CGRectNull addToSuperview:nil];
}
+ (instancetype)blockedButtonWithTapHandler:(void(^)())tapHandler frame:(CGRect)frame {
return [self blockedButtonWithTapHandler:tapHandler frame:frame addToSuperview:nil];
}
+ (instancetype)blockedButtonWithTapHandler:(void(^)())tapHandler frame:(CGRect)frame addToSuperview:(UIView *)superview {
GKBlockedButton *button = [GKBlockedButton new];
button.tapHandler = tapHandler;
if (!CGRectIsNull(frame)) {
button.frame = frame;
}
if (superview) {
[superview addSubview:button];
}
return button;
}
@end
大概就是这样了,如果有不正确的地方欢迎批评指正。不会用的可以看下DEMO中的方法~
最后放上代码链接:https://github.com/ChrisCaixx/blockedBarButtonItem.git