Last active
June 16, 2019 01:46
-
-
Save KittenYang/1dc93bbb07c5d97facea806fec3dcf44 to your computer and use it in GitHub Desktop.
使用 Block 创建 UIGestureRecognizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
请问有没有使用的demo啊,我这加了之后一直报错,谢谢