Instantly share code, notes, and snippets.

Embed
What would you like to do?
使用 Block 创建 UIGestureRecognizer
static const int target_key;
@implementation UIGestureRecognizer (Block)
+(instancetype)nvm_gestureRecognizerWithActionBlock:(NVMGestureBlock)block {
return [[self alloc]initWithActionBlock:block];
}
- (instancetype)initWithActionBlock:(NVMGestureBlock)block {
self = [self init];
[self addActionBlock:block];
[self addTarget:self action:@selector(invoke:)];
return self;
}
- (void)addActionBlock:(NVMGestureBlock)block {
if (block) {
objc_setAssociatedObject(self, &target_key, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
}
- (void)invoke:(id)sender {
NVMGestureBlock block = objc_getAssociatedObject(self, &target_key);
if (block) {
block(sender);
}
}
@end
@zhaoxiaobao

This comment has been minimized.

zhaoxiaobao commented Apr 12, 2016

很实用

@wendf

This comment has been minimized.

wendf commented Jul 12, 2016

请问有没有使用的demo啊,我这加了之后一直报错,谢谢

@devfalme

This comment has been minimized.

devfalme commented Dec 12, 2016

放一下.h可以吗, 自己写了以后不管怎么都不走invoke

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment