-
Notifications
You must be signed in to change notification settings - Fork 7
/
TiUIView+GestureRecognizer.m
248 lines (212 loc) · 8.62 KB
/
TiUIView+GestureRecognizer.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//
// TiUIView+GestureRecognizer.m
// gesturerecognizer
//
// Created by KATAOKA,Atsushi on 11/09/24.
// Copyright 2011 MARSHMALLOW MACHINE. All rights reserved.
//
#import "TiUtils.h"
#import "TiUIView+GestureRecognizer.h"
@interface JPMSMCGESTURERECOGNIZER_FIXCATEGORYBUG_TIUIVIEW @end
@implementation JPMSMCGESTURERECOGNIZER_FIXCATEGORYBUG_TIUIVIEW @end
#define RECOGNIZE_SIMULTANEOUSLY_ROTATE (1 << 16)
#define RECOGNIZE_SIMULTANEOUSLY_PAN (1 << 15)
#define RECOGNIZE_SIMULTANEOUSLY_PINCH (1 << 14)
#define RECOGNIZE_SIMULTANEOUSLY_ALL (RECOGNIZE_SIMULTANEOUSLY_ROTATE|RECOGNIZE_SIMULTANEOUSLY_PAN|RECOGNIZE_SIMULTANEOUSLY_PINCH)
@implementation TiUIView (TiUIView_GestureRecognizer)
- (void)setRecognizeSimultaneously_:(id)value
{
if([value isKindOfClass:[NSNumber class]]){
BOOL value_ = [value boolValue];
if(value_)
{
NSLog(@"[DEBUG] RecognizeSimultaneously to true.");
for(UIGestureRecognizer *gesture in self.gestureRecognizers)
{
gesture.delegate = self;
}
self.tag |= RECOGNIZE_SIMULTANEOUSLY_ALL;
}
else
{
NSLog(@"[DEBUG] RecognizeSimultaneously to false.");
for(UIGestureRecognizer *gesture in self.gestureRecognizers)
{
gesture.delegate = nil;
}
self.tag &= ~RECOGNIZE_SIMULTANEOUSLY_ALL;
}
}else if([value isKindOfClass:[NSString class]]){
self.tag &= ~RECOGNIZE_SIMULTANEOUSLY_ALL;
NSArray *components = [value componentsSeparatedByString:@","];
for(NSString *component in components){
if([component isEqualToString:@"pinching"]){
self.tag |= RECOGNIZE_SIMULTANEOUSLY_PINCH;
NSLog(@"[DEBUG] RecognizeSimultaneously to pinch.");
}
if([component isEqualToString:@"pan"]){
self.tag |= RECOGNIZE_SIMULTANEOUSLY_PAN;
NSLog(@"[DEBUG] RecognizeSimultaneously to pan.");
}
if([component isEqualToString:@"rotate"]){
self.tag |= RECOGNIZE_SIMULTANEOUSLY_ROTATE;
NSLog(@"[DEBUG] RecognizeSimultaneously to rotate.");
}
}
}else{
NSLog(@"[WARN] recognizeSimultaneously property is invalid.");
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
BOOL result = NO;
if([otherGestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]]){
result = (self.tag & RECOGNIZE_SIMULTANEOUSLY_PINCH) ? YES : NO;
}
if([otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]){
result = (self.tag & RECOGNIZE_SIMULTANEOUSLY_PAN) ? YES : NO;
}
if([otherGestureRecognizer isKindOfClass:[UIRotationGestureRecognizer class]]){
result = (self.tag & RECOGNIZE_SIMULTANEOUSLY_ROTATE) ? YES : NO;
}
//NSLog(@"[DEBUG] recognizer:%@ other:%@ result:%d tag:%d", [gestureRecognizer class], [otherGestureRecognizer class], result, self.tag);
return result;
}
- (void)handleRotationGesture:(UIRotationGestureRecognizer *)sender
{
NSDictionary *args = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:[sender rotation]], @"rotation",
[NSNumber numberWithFloat:[sender velocity]], @"velocity", nil];
if([self.proxy _hasListeners:@"rotate"]){
[self.proxy fireEvent:@"rotate" withObject:args];
}
if(sender.state == UIGestureRecognizerStateEnded &&
[self.proxy _hasListeners:@"rotateend"]){
[self.proxy fireEvent:@"rotateend"];
}
}
- (void)setRotateGesture_:(id)value
{
ENSURE_SINGLE_ARG(value, NSNumber);
BOOL value_ = [value boolValue];
if(value_){
for(UIGestureRecognizer *gesure in self.gestureRecognizers){
if([gesure isKindOfClass:[UIRotationGestureRecognizer class]]){
return;
}
}
UIRotationGestureRecognizer *rotationGesture =[[UIRotationGestureRecognizer alloc] initWithTarget:self
action:@selector(handleRotationGesture:)];
[self addGestureRecognizer:rotationGesture];
if(self.tag & RECOGNIZE_SIMULTANEOUSLY_ROTATE)
{
rotationGesture.delegate = self;
}
[rotationGesture release];
} else{
UIGestureRecognizer *rotationGesture = nil;
for(UIGestureRecognizer *gesure in self.gestureRecognizers){
if([gesure isKindOfClass:[UIRotationGestureRecognizer class]]){
rotationGesture = gesure;
break;
}
}
rotationGesture.delegate = nil;
if(rotationGesture){
[self removeGestureRecognizer:rotationGesture];
}
}
}
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)sender
{
NSDictionary *args = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:[sender scale]], @"scale",
[NSNumber numberWithFloat:[sender velocity]], @"velocity", nil];
if([self.proxy _hasListeners:@"pinching"]){
[self.proxy fireEvent:@"pinching" withObject:args];
}
if(sender.state == UIGestureRecognizerStateEnded &&
[self.proxy _hasListeners:@"pinchingend"]){
[self.proxy fireEvent:@"pinchingend"];
}
}
- (void)setPinchingGesture_:(id)value
{
ENSURE_SINGLE_ARG(value, NSNumber);
BOOL value_ = [value boolValue];
if(value_){
for(UIGestureRecognizer *gesure in self.gestureRecognizers){
if([gesure isKindOfClass:[UIPinchGestureRecognizer class]]){
return;
}
}
UIPinchGestureRecognizer *pinchGesture =[[UIPinchGestureRecognizer alloc] initWithTarget:self
action:@selector(handlePinchGesture:)];
[self addGestureRecognizer:pinchGesture];
if(self.tag & RECOGNIZE_SIMULTANEOUSLY_PINCH)
{
pinchGesture.delegate = self;
}
[pinchGesture release];
} else{
UIGestureRecognizer *pinchGesture = nil;
for(UIGestureRecognizer *gesure in self.gestureRecognizers){
if([gesure isKindOfClass:[UIPinchGestureRecognizer class]]){
pinchGesture = gesure;
break;
}
}
pinchGesture.delegate = self;
if(pinchGesture){
[self removeGestureRecognizer:pinchGesture];
}
}
}
- (void)handlePanGesture:(UIPanGestureRecognizer *)sender
{
CGPoint translation = [sender translationInView:self.superview];
CGPoint velocity = [sender velocityInView:self.superview];
NSDictionary *args = [NSDictionary dictionaryWithObjectsAndKeys:
[[[TiPoint alloc] initWithPoint:translation] autorelease], @"translation",
[[[TiPoint alloc] initWithPoint:velocity] autorelease], @"velocity", nil];
if([self.proxy _hasListeners:@"pan"]){
[self.proxy fireEvent:@"pan" withObject:args];
}
if(sender.state == UIGestureRecognizerStateEnded &&
[self.proxy _hasListeners:@"panend"]){
[self.proxy fireEvent:@"panend"];
}
}
- (void)setPanGesture_:(id)value
{
ENSURE_SINGLE_ARG(value, NSNumber);
BOOL value_ = [value boolValue];
if(value_){
for(UIGestureRecognizer *gesure in self.gestureRecognizers){
if([gesure isKindOfClass:[UIPanGestureRecognizer class]]){
return;
}
}
UIPanGestureRecognizer *panGesture =[[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(handlePanGesture:)];
[self addGestureRecognizer:panGesture];
if(self.tag & RECOGNIZE_SIMULTANEOUSLY_PAN)
{
panGesture.delegate = self;
}
[panGesture release];
} else{
UIGestureRecognizer *panGesture = nil;
for(UIGestureRecognizer *gesure in self.gestureRecognizers){
if([gesure isKindOfClass:[UIPanGestureRecognizer class]]){
panGesture = gesure;
break;
}
}
panGesture.delegate = nil;
if(panGesture){
[self removeGestureRecognizer:panGesture];
}
}
}
@end