美文网首页
iOS-通过类方法为UIView添加带block的点击事件,

iOS-通过类方法为UIView添加带block的点击事件,

作者: 管你爱不爱 | 来源:发表于2017-03-11 10:52 被阅读1189次
#import@interface UIView (ESAddTapGestureRecognizer)

@property (nonatomic,assign) void(^block)(NSInteger tag);

- (void)addTapGestureRecognizerWithDelegate:(id)tapGestureDelegate Block:(void(^)(NSInteger tag))block;

@end
#import "UIView+ESAddTapGestureRecognizer.h"#import@implementation UIView (ESAddTapGestureRecognizer)

- (void)addTapGestureRecognizerWithDelegate:(id)tapGestureDelegate Block:(void (^)(NSInteger))block {

self.block = block;

UITapGestureRecognizer *tag = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tagClick)];

[self addGestureRecognizer:tag];

if (tapGestureDelegate) {

tag.delegate = tapGestureDelegate;

}

self.userInteractionEnabled = YES;

}

- (void)tagClick {

if (self.block) {

self.block(self.tag);

}

}

- (void)setBlock:(void (^)(NSInteger tag))block

{

objc_setAssociatedObject(self, @selector(block), block, OBJC_ASSOCIATION_COPY_NONATOMIC);

}

- (void(^)(NSInteger tag))block

{

return objc_getAssociatedObject(self, @selector(block));

}

@end

相关文章

网友评论

      本文标题:iOS-通过类方法为UIView添加带block的点击事件,

      本文链接:https://www.haomeiwen.com/subject/reeagttx.html