-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIView+CNGesture.m
130 lines (110 loc) · 5.67 KB
/
UIView+CNGesture.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//
// UIView+CNGesture.m
// Cievon
//
// Created by cievon on 2017/10/27.
// Copyright © 2017年 cievon. All rights reserved.
//
#import "UIView+CNGesture.h"
#import "objc/runtime.h"
#define SuppressPerformSelectorLeakWarning(Stuff) \
do { \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
Stuff; \
_Pragma("clang diagnostic pop") \
} while (0)
static char const * kTargetKey = "kTargetKey";
static char const * kTapActionKey = "kTapActionKey";
static char const * kDBActionKey = "kDBActionKey";
static char const * kSimpleTapRecognizerKey = "kSimpleTapRecognizerKey";
static char const * kLongPressStartActionKey = "kLongPressStartActionKey";
static char const * kLongPressEndActionKey = "kLongPressEndActionKey";
static char const * kLongPressCancelActionKey = "kLongPressCancelActionKey";
@implementation UIView (CNGesture)
- (void)cn_addTarget:(id)target action:(SEL)action {
self.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
tap.numberOfTapsRequired = 1;
[self addGestureRecognizer:tap];
objc_setAssociatedObject(self, kSimpleTapRecognizerKey, tap, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self, kTargetKey, target, OBJC_ASSOCIATION_ASSIGN);
objc_setAssociatedObject(self, kTapActionKey, NSStringFromSelector(action), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)cn_addDBclick:(id)target action:(SEL)action {
self.userInteractionEnabled = YES;
UITapGestureRecognizer *dbTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
dbTap.numberOfTapsRequired = 2;
[self addGestureRecognizer:dbTap];
objc_setAssociatedObject(self, kTargetKey, target, OBJC_ASSOCIATION_ASSIGN);
objc_setAssociatedObject(self, kDBActionKey, NSStringFromSelector(action), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
UITapGestureRecognizer *simpleTapGesture = objc_getAssociatedObject(self, kSimpleTapRecognizerKey);
if (simpleTapGesture) {
[simpleTapGesture requireGestureRecognizerToFail:dbTap];
}
}
- (void)cn_addLongPressTarget:(id)target action:(SEL)action event:(CNLongPressEvents)event {
self.userInteractionEnabled = YES;
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
[self addGestureRecognizer:longPress];
objc_setAssociatedObject(self, kTargetKey, target, OBJC_ASSOCIATION_ASSIGN);
switch (event) {
case CNLongPressEventsStart:
objc_setAssociatedObject(self, kLongPressStartActionKey, NSStringFromSelector(action), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
break;
case CNLongPressEventsEnd:
objc_setAssociatedObject(self, kLongPressEndActionKey, NSStringFromSelector(action), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
break;
case CNLongPressEventsCancel:
objc_setAssociatedObject(self, kLongPressCancelActionKey, NSStringFromSelector(action), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
break;
default:
break;
}
}
- (void)tapGesture:(UITapGestureRecognizer *)gesture {
const char *actionKey;
switch (gesture.numberOfTapsRequired) {
case 1:
{
gesture.enabled = NO;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
gesture.enabled = YES;
});
actionKey = kTapActionKey;
}
break;
case 2:
{
actionKey = kDBActionKey;
}
break;
default:
break;
}
id target = objc_getAssociatedObject(self, kTargetKey);
SEL tapAction = NSSelectorFromString(objc_getAssociatedObject(self, actionKey));
if (!target && tapAction) return;
if (gesture.state == UIGestureRecognizerStateEnded) {
SuppressPerformSelectorLeakWarning([target performSelector:tapAction withObject:self]);
}
}
- (void)longPressGesture:(UILongPressGestureRecognizer *)gesture {
SuppressPerformSelectorLeakWarning(
id target = objc_getAssociatedObject(self, kTargetKey);
SEL longPressStartAction = NSSelectorFromString(objc_getAssociatedObject(self, kLongPressStartActionKey));
SEL longPressEndAction = NSSelectorFromString(objc_getAssociatedObject(self, kLongPressEndActionKey));
SEL longPressCancelAction = NSSelectorFromString(objc_getAssociatedObject(self, kLongPressCancelActionKey));
if (gesture.state == UIGestureRecognizerStateBegan) {
if (!(target && longPressStartAction)) return;
[target performSelector:longPressStartAction withObject:self];
}else if (gesture.state == UIGestureRecognizerStateEnded) {
if (!(target && longPressEndAction)) return;
[target performSelector:longPressEndAction withObject:self];
}else if (gesture.state == UIGestureRecognizerStateCancelled){
if (!(target && longPressCancelAction)) return;
[target performSelector:longPressCancelAction withObject:self];
}
);
}
@end