This repository has been archived by the owner on Jun 2, 2018. It is now read-only.
/
UIView+BlocksKit.m
executable file
·99 lines (79 loc) · 3 KB
/
UIView+BlocksKit.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// UIView+BlocksKit.m
// BlocksKit
//
#import "UIView+BlocksKit.h"
#import "NSObject+AssociatedObjects.h"
#import "UIGestureRecognizer+BlocksKit.h"
#import "NSArray+BlocksKit.h"
static char *kViewTouchDownBlockKey = "UIViewTouchDownBlock";
static char *kViewTouchMoveBlockKey = "UIViewTouchMoveBlock";
static char *kViewTouchUpBlockKey = "UIViewTouchUpBlock";
@implementation UIView (BlocksKit)
- (void)whenTouches:(NSUInteger)numberOfTouches tapped:(NSUInteger)numberOfTaps handler:(BKBlock)block {
self.userInteractionEnabled = YES;
block = [block copy];
UITapGestureRecognizer *gesture = [UITapGestureRecognizer recognizerWithHandler:^(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location) {
block();
}];
BK_RELEASE(block);
[gesture setNumberOfTouchesRequired:numberOfTouches];
[gesture setNumberOfTapsRequired:numberOfTaps];
[[self.gestureRecognizers select:^BOOL(id obj) {
if ([obj isKindOfClass:[UITapGestureRecognizer class]]) {
BOOL rightTouches = ([(UITapGestureRecognizer *)obj numberOfTouchesRequired] == numberOfTouches);
BOOL rightTaps = ([(UITapGestureRecognizer *)obj numberOfTapsRequired] == numberOfTaps);
return (rightTouches && rightTaps);
}
return NO;
}] each:^(id obj) {
[gesture requireGestureRecognizerToFail:(UITapGestureRecognizer *)obj];
}];
[self addGestureRecognizer:gesture];
}
- (void)whenTapped:(BKBlock)block {
[self whenTouches:1 tapped:1 handler:block];
}
- (void)whenDoubleTapped:(BKBlock)block {
[self whenTouches:2 tapped:1 handler:block];
}
- (void)whenTouchedDown:(BKTouchBlock)block {
self.userInteractionEnabled = YES;
if (!block)
block = nil;
[self associateCopyOfValue:block withKey:kViewTouchDownBlockKey];
}
- (void)whenTouchMove:(BKTouchBlock)block {
self.userInteractionEnabled = YES;
if (!block)
block = nil;
[self associateCopyOfValue:block withKey:kViewTouchMoveBlockKey];
}
- (void)whenTouchedUp:(BKTouchBlock)block {
self.userInteractionEnabled = YES;
if (!block)
block = nil;
[self associateCopyOfValue:block withKey:kViewTouchUpBlockKey];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
BKTouchBlock block = [self associatedValueForKey:kViewTouchDownBlockKey];
if (block)
block(touches, event);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
BKTouchBlock block = [self associatedValueForKey:kViewTouchMoveBlockKey];
if (block)
block(touches, event);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
BKTouchBlock block = [self associatedValueForKey:kViewTouchUpBlockKey];
if (block)
block(touches, event);
}
- (void)eachSubview:(BKViewBlock)block {
[self.subviews each:(BKSenderBlock)block];
}
@end