This repository has been archived by the owner on Oct 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTouchSynthesis.m
227 lines (197 loc) · 4.97 KB
/
TouchSynthesis.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
//
// TouchSynthesis.m
// SelfTesting
//
// Created by Matt Gallagher on 23/11/08.
// Copyright 2008 Matt Gallagher. All rights reserved.
//
// Permission is given to use this source code file, free of charge, in any
// project, commercial or otherwise, entirely at your risk, with the condition
// that any redistribution (in part or whole) of source code must retain
// this copyright and permission notice. Attribution in compiled projects is
// appreciated but not required.
//
#import "TouchSynthesis.h"
@implementation UITouch (Synthesize)
//
// initInView
//
// Creats a UITouch, centered on the specified view, in the view's window.
//
- (id)initInView:(UIView *)view
{
return [self initInView:view hitTest:YES];
}
//
// initInView:hitTest
//
// Creats a UITouch, centered on the specified view, in the view's window.
// Determines the target view by either performing a hit test, or just
// forcing the tap to land on the passed-in view.
//
- (id)initInView:(UIView *)view hitTest:(BOOL)hitTest
{
self = [super init];
if (self != nil)
{
CGRect frameInWindow;
if ([view isKindOfClass:[UIWindow class]])
{
frameInWindow = view.frame;
}
else
{
frameInWindow =
[view.window convertRect:view.frame fromView:view.superview];
}
_tapCount = 1;
_locationInWindow =
CGPointMake(
frameInWindow.origin.x + 0.5 * frameInWindow.size.width,
frameInWindow.origin.y + 0.5 * frameInWindow.size.height);
_previousLocationInWindow = _locationInWindow;
UIView *target = hitTest ?
[view.window hitTest:_locationInWindow withEvent:nil] :
view;
_window = [view.window retain];
_view = [target retain];
_phase = UITouchPhaseBegan;
_touchFlags._firstTouchForView = 1;
_touchFlags._isTap = 1;
_timestamp = [NSDate timeIntervalSinceReferenceDate];
}
return self;
}
//
// setPhase:
//
// Setter to allow access to the _phase member.
//
- (void)setPhase:(UITouchPhase)phase
{
_phase = phase;
_timestamp = [NSDate timeIntervalSinceReferenceDate];
}
//
// setLocationInWindow:
//
// Setter to allow access to the _locationInWindow member.
//
- (void)setLocationInWindow:(CGPoint)location
{
_previousLocationInWindow = _locationInWindow;
_locationInWindow = location;
_timestamp = [NSDate timeIntervalSinceReferenceDate];
}
//
// moveLocationInWindow:
//
// Adjusts location to the right
//
- (void)moveLocationInWindow
{
CGPoint moveTo = CGPointMake(_locationInWindow.x + 200, _locationInWindow.y);
[self setLocationInWindow:moveTo];
}
//
// moveLocationInWindowLeft:
//
// Adjusts location to the LEFT
//
- (void)moveLocationInWindowLeft
{
CGPoint moveTo = CGPointMake(_locationInWindow.x - 200, _locationInWindow.y);
[self setLocationInWindow:moveTo];
}
@end
//
// GSEvent is an undeclared object. We don't need to use it ourselves but some
// Apple APIs (UIScrollView in particular) require the x and y fields to be present.
//
@interface GSEventProxy : NSObject
{
@public
unsigned int flags;
unsigned int type;
unsigned int ignored1;
float x1;
float y1;
float x2;
float y2;
unsigned int ignored2[10];
unsigned int ignored3[7];
float sizeX;
float sizeY;
float x3;
float y3;
unsigned int ignored4[3];
}
@end
@implementation GSEventProxy
@end
//
// PublicEvent
//
// A dummy class used to gain access to UIEvent's private member variables.
// If UIEvent changes at all, this will break.
//
@interface PublicEvent : NSObject
{
@public
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 31000
GSEventProxy *_event;
#endif
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 30000
NSTimeInterval _timestamp;
#endif
NSMutableSet *_touches;
CFMutableDictionaryRef _keyedTouches;
}
@end
@implementation PublicEvent
@end
@interface UIEvent (Creation)
- (id)_initWithEvent:(GSEventProxy *)fp8 touches:(id)fp12;
@end
//
// UIEvent (Synthesize)
//
// A category to allow creation of a touch event.
//
@implementation UIEvent (Synthesize)
- (id)initWithTouch:(UITouch *)touch
{
CGPoint location = [touch locationInView:touch.window];
GSEventProxy *gsEventProxy = [[GSEventProxy alloc] init];
gsEventProxy->x1 = location.x;
gsEventProxy->y1 = location.y;
gsEventProxy->x2 = location.x;
gsEventProxy->y2 = location.y;
gsEventProxy->x3 = location.x;
gsEventProxy->y3 = location.y;
gsEventProxy->sizeX = 1.0;
gsEventProxy->sizeY = 1.0;
gsEventProxy->flags = ([touch phase] == UITouchPhaseEnded) ? 0x1010180 : 0x3010180;
gsEventProxy->type = 3001;
//
// On SDK versions 3.0 and greater, we need to reallocate as a
// UITouchesEvent.
//
Class touchesEventClass = objc_getClass("UITouchesEvent");
if (touchesEventClass && ![[self class] isEqual:touchesEventClass])
{
[self release];
self = [touchesEventClass alloc];
}
self = [self _initWithEvent:gsEventProxy touches:[NSSet setWithObject:touch]];
return self;
}
- (void)moveLocation
{
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 30000
PublicEvent *publicEvent = (PublicEvent *)self;
publicEvent->_timestamp = [NSDate timeIntervalSinceReferenceDate];
publicEvent->_event->x += 20;
#endif
}
@end