forked from kgn/BBlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIGestureRecognizer+BBlock.m
51 lines (39 loc) · 1.44 KB
/
UIGestureRecognizer+BBlock.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// UIGestureRecognizer+BBlock.m
// BBlock
//
// Created by David Keegan on 12/29/11.
// Copyright (c) 2011-12 David Keegan. All rights reserved.
//
#import "UIGestureRecognizer+BBlock.h"
#import <objc/runtime.h>
static char UIGestureRecognizerBBlockKey;
@implementation UIGestureRecognizer(BBlock)
- (instancetype)initWithBlock:(UIGestureRecognizerBBlock)block{
if((self = [self initWithTarget:self action:@selector(gestureAction:)])){
objc_setAssociatedObject(self, &UIGestureRecognizerBBlockKey,
block, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
return self;
}
+ (instancetype)gestureRecognizerWithBlock:(UIGestureRecognizerBBlock)block{
return [[[self class] alloc] initWithBlock:block];
}
- (void)gestureAction:(id)gestureRecognizer{
UIGestureRecognizerBBlock block = objc_getAssociatedObject(self, &UIGestureRecognizerBBlockKey);
if(block){
block(gestureRecognizer);
}
}
@end
@implementation UISwipeGestureRecognizer(BBlock)
- (instancetype)initWithDirection:(UISwipeGestureRecognizerDirection)direction andBlock:(UIGestureRecognizerBBlock)block{
if((self = [self initWithBlock:block])){
self.direction = direction;
}
return self;
}
+ (instancetype)gestureRecognizerWithDirection:(UISwipeGestureRecognizerDirection)direction andBlock:(UIGestureRecognizerBBlock)block{
return [[[self class] alloc] initWithDirection:direction andBlock:block];
}
@end