标签:
// 自定义导航栏左边按钮 self.navigationItem.leftBarButtonItem = [JQBlockedBarButtonItem blockedBarButtonItemWithTitle:@"返回" eventHandler:^{ [weakSelf.navigationController popViewControllerAnimated:YES]; }];
// 声明文件提供常用的接口 @interface JQBlockedBarButtonItem : 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 "JQBlockedBarButtonItem.h" @interface JQBlockedBarButtonItem () @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
// 自定义按钮,特意选了个最长的~ CGRect rect = CGRectMake(100, 100, 100, 100); __weak typeof(self) weakSelf = self; JQBlockedButton *blockedBtn = [JQBlockedButton blockedButtonWithTapHandler:^{ NSLog(@"点击了按钮"); [weakSelf.navigationController pushViewController:[TestViewController new] animated:YES]; } frame:rect addToSuperview:self.view]; blockedBtn.backgroundColor = [UIColor redColor];
@interface JQBlockedButton : 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 JQBlockedButton - (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
第十篇、自定义UIBarButtonItem和UIButton block回调
标签:
原文地址:http://www.cnblogs.com/HJQ2016/p/5794112.html