forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgestures.ios.ts
More file actions
472 lines (388 loc) · 17.1 KB
/
gestures.ios.ts
File metadata and controls
472 lines (388 loc) · 17.1 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
import common = require("./gestures-common");
import definition = require("ui/gestures");
import view = require("ui/core/view");
import observable = require("data/observable");
import trace = require("trace");
import types = require("utils/types");
global.moduleMerge(common, exports);
class UIGestureRecognizerDelegateImpl extends NSObject implements UIGestureRecognizerDelegate {
public static ObjCProtocols = [UIGestureRecognizerDelegate];
public gestureRecognizerShouldRecognizeSimultaneouslyWithGestureRecognizer(gestureRecognizer: UIGestureRecognizer, otherGestureRecognizer: UIGestureRecognizer): boolean {
return true;
}
}
var recognizerDelegateInstance: UIGestureRecognizerDelegateImpl = <UIGestureRecognizerDelegateImpl>UIGestureRecognizerDelegateImpl.new();
class UIGestureRecognizerImpl extends NSObject {
private _owner: WeakRef<GesturesObserver>;
private _type: any;
private _callback: Function;
private _context: any;
public static initWithOwnerTypeCallback(owner: WeakRef<GesturesObserver>, type: any, callback?: Function, thisArg?: any): UIGestureRecognizerImpl {
let handler = <UIGestureRecognizerImpl>UIGestureRecognizerImpl.new();
handler._owner = owner;
handler._type = type;
if (callback) {
handler._callback = callback;
}
if (thisArg) {
handler._context = thisArg;
}
return handler;
}
public static ObjCExposedMethods = {
"recognize": { returns: interop.types.void, params: [UIGestureRecognizer] }
};
public recognize(recognizer: UIGestureRecognizer): void {
let owner = this._owner.get();
let callback = this._callback ? this._callback : (owner ? owner.callback : null);
let typeParam = this._type;
let target = owner ? owner.target : undefined;
var args = {
type: typeParam,
view: target,
ios: recognizer,
android: undefined,
object: target,
eventName: definition.toString(typeParam),
};
if (callback) {
callback.call(this._context, args);
}
}
}
export class GesturesObserver extends common.GesturesObserver {
private _recognizers: {};
private _onTargetLoaded: (data: observable.EventData) => void;
private _onTargetUnloaded: (data: observable.EventData) => void;
constructor(target: view.View, callback: (args: definition.GestureEventData) => void, context: any) {
super(target, callback, context);
this._recognizers = {};
}
public observe(type: definition.GestureTypes) {
if (this.target) {
this.type = type;
this._onTargetLoaded = args => {
trace.write(this.target + ".target loaded. _nativeView:" + this.target._nativeView, "gestures");
this._attach(this.target, type);
};
this._onTargetUnloaded = args => {
trace.write(this.target + ".target unloaded. _nativeView:" + this.target._nativeView, "gestures");
this._detach();
};
this.target.on(view.View.loadedEvent, this._onTargetLoaded);
this.target.on(view.View.unloadedEvent, this._onTargetUnloaded);
if (this.target.isLoaded) {
this._attach(this.target, type);
}
}
}
private _attach(target: view.View, type: definition.GestureTypes) {
trace.write(target + "._attach() _nativeView:" + target._nativeView, "gestures");
this._detach();
if (target && target._nativeView && target._nativeView.addGestureRecognizer) {
var nativeView = <UIView>target._nativeView;
if (type & definition.GestureTypes.tap) {
nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.tap));
}
if (type & definition.GestureTypes.doubleTap) {
var r = <UITapGestureRecognizer>this._createRecognizer(definition.GestureTypes.doubleTap);
r.numberOfTapsRequired = 2;
nativeView.addGestureRecognizer(r);
}
if (type & definition.GestureTypes.pinch) {
nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.pinch, args => {
this._executeCallback(_getPinchData(args));
}));
}
if (type & definition.GestureTypes.pan) {
nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.pan, args => {
this._executeCallback(_getPanData(args, target._nativeView));
}));
}
if (type & definition.GestureTypes.swipe) {
nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.swipe, args => {
this._executeCallback(_getSwipeData(args));
}, UISwipeGestureRecognizerDirection.UISwipeGestureRecognizerDirectionDown));
nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.swipe, args => {
this._executeCallback(_getSwipeData(args));
}, UISwipeGestureRecognizerDirection.UISwipeGestureRecognizerDirectionLeft));
nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.swipe, args => {
this._executeCallback(_getSwipeData(args));
}, UISwipeGestureRecognizerDirection.UISwipeGestureRecognizerDirectionRight));
nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.swipe, args => {
this._executeCallback(_getSwipeData(args));
}, UISwipeGestureRecognizerDirection.UISwipeGestureRecognizerDirectionUp));
}
if (type & definition.GestureTypes.rotation) {
nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.rotation, args => {
this._executeCallback(_getRotationData(args));
}));
}
if (type & definition.GestureTypes.longPress) {
nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.longPress));
}
if (type & definition.GestureTypes.touch) {
nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.touch));
}
}
}
private _detach() {
trace.write(this.target + "._detach() _nativeView:" + this.target._nativeView, "gestures");
if (this.target && this.target._nativeView) {
for (var name in this._recognizers) {
if (this._recognizers.hasOwnProperty(name)) {
var item = <RecognizerCache>this._recognizers[name];
this.target._nativeView.removeGestureRecognizer(item.recognizer);
item.recognizer = null;
item.target = null;
}
}
this._recognizers = {};
}
}
public disconnect() {
this._detach();
if (this.target) {
this.target.off(view.View.loadedEvent, this._onTargetLoaded);
this.target.off(view.View.unloadedEvent, this._onTargetUnloaded);
this._onTargetLoaded = null;
this._onTargetUnloaded = null;
}
// clears target, context and callback references
super.disconnect();
}
public _executeCallback(args: definition.GestureEventData) {
if (this.callback) {
this.callback.call(this.context, args);
}
}
private _createRecognizer(type: definition.GestureTypes, callback?: (args: definition.GestureEventData) => void, swipeDirection?: UISwipeGestureRecognizerDirection): UIGestureRecognizer {
var recognizer: UIGestureRecognizer;
var name = definition.toString(type);
var target = _createUIGestureRecognizerTarget(this, type, callback, this.context);
var recognizerType = _getUIGestureRecognizerType(type);
if (recognizerType) {
recognizer = recognizerType.alloc().initWithTargetAction(target, "recognize");
if (type === definition.GestureTypes.swipe && swipeDirection) {
name = name + swipeDirection.toString();
(<UISwipeGestureRecognizer>recognizer).direction = swipeDirection;
}
else if (type === definition.GestureTypes.touch) {
(<TouchGestureRecognizer>recognizer).observer = this;
}
if (recognizer) {
recognizer.delegate = recognizerDelegateInstance;
this._recognizers[name] = <RecognizerCache>{ recognizer: recognizer, target: target };
}
}
return recognizer;
}
}
function _createUIGestureRecognizerTarget(owner: GesturesObserver, type: definition.GestureTypes, callback?: (args: definition.GestureEventData) => void, context?: any): any {
return UIGestureRecognizerImpl.initWithOwnerTypeCallback(new WeakRef(owner), type, callback, context);
}
interface RecognizerCache {
recognizer: UIGestureRecognizer;
target: any;
}
function _getUIGestureRecognizerType(type: definition.GestureTypes): any {
var nativeType = null;
if (type === definition.GestureTypes.tap) {
nativeType = UITapGestureRecognizer;
} else if (type === definition.GestureTypes.doubleTap) {
nativeType = UITapGestureRecognizer;
} else if (type === definition.GestureTypes.pinch) {
nativeType = UIPinchGestureRecognizer;
} else if (type === definition.GestureTypes.pan) {
nativeType = UIPanGestureRecognizer;
} else if (type === definition.GestureTypes.swipe) {
nativeType = UISwipeGestureRecognizer;
} else if (type === definition.GestureTypes.rotation) {
nativeType = UIRotationGestureRecognizer;
} else if (type === definition.GestureTypes.longPress) {
nativeType = UILongPressGestureRecognizer;
} else if (type === definition.GestureTypes.touch) {
nativeType = TouchGestureRecognizer;
}
return nativeType;
}
function getState(recognizer: UIGestureRecognizer) {
if (recognizer.state === UIGestureRecognizerState.UIGestureRecognizerStateBegan) {
return common.GestureStateTypes.began;
} else if (recognizer.state === UIGestureRecognizerState.UIGestureRecognizerStateCancelled) {
return common.GestureStateTypes.cancelled;
} else if (recognizer.state === UIGestureRecognizerState.UIGestureRecognizerStateChanged) {
return common.GestureStateTypes.changed;
} else if (recognizer.state === UIGestureRecognizerState.UIGestureRecognizerStateEnded) {
return common.GestureStateTypes.ended;
}
}
function _getSwipeDirection(direction: UISwipeGestureRecognizerDirection): definition.SwipeDirection {
if (direction === UISwipeGestureRecognizerDirection.UISwipeGestureRecognizerDirectionDown) {
return definition.SwipeDirection.down;
} else if (direction === UISwipeGestureRecognizerDirection.UISwipeGestureRecognizerDirectionLeft) {
return definition.SwipeDirection.left;
} else if (direction === UISwipeGestureRecognizerDirection.UISwipeGestureRecognizerDirectionRight) {
return definition.SwipeDirection.right;
} else if (direction === UISwipeGestureRecognizerDirection.UISwipeGestureRecognizerDirectionUp) {
return definition.SwipeDirection.up;
}
}
function _getPinchData(args: definition.GestureEventData): definition.PinchGestureEventData {
var recognizer = <UIPinchGestureRecognizer>args.ios;
var center = recognizer.locationInView(args.view._nativeView);
return <definition.PinchGestureEventData>{
type: args.type,
view: args.view,
ios: args.ios,
android: undefined,
scale: recognizer.scale,
getFocusX: () => { return center.x; },
getFocusY: () => { return center.y; },
object: args.view,
eventName: definition.toString(args.type),
state: getState(recognizer)
};
}
function _getSwipeData(args: definition.GestureEventData): definition.SwipeGestureEventData {
var recognizer = <UISwipeGestureRecognizer>args.ios;
return <definition.SwipeGestureEventData>{
type: args.type,
view: args.view,
ios: args.ios,
android: undefined,
direction: _getSwipeDirection(recognizer.direction),
object: args.view,
eventName: definition.toString(args.type),
};
}
function _getPanData(args: definition.GestureEventData, view: UIView): definition.PanGestureEventData {
var recognizer = <UIPanGestureRecognizer>args.ios;
return <definition.PanGestureEventData>{
type: args.type,
view: args.view,
ios: args.ios,
android: undefined,
deltaX: recognizer.translationInView(view).x,
deltaY: recognizer.translationInView(view).y,
object: args.view,
eventName: definition.toString(args.type),
state: getState(recognizer)
};
}
function _getRotationData(args: definition.GestureEventData): definition.RotationGestureEventData {
var recognizer = <UIRotationGestureRecognizer>args.ios;
return <definition.RotationGestureEventData>{
type: args.type,
view: args.view,
ios: args.ios,
android: undefined,
rotation: recognizer.rotation * (180.0 / Math.PI),
object: args.view,
eventName: definition.toString(args.type),
state: getState(recognizer)
};
}
class TouchGestureRecognizer extends UIGestureRecognizer {
public observer: GesturesObserver;
private _eventData: TouchGestureEventData;
touchesBeganWithEvent(touches: NSSet, event: any): void {
this.executeCallback(common.TouchAction.down, touches, event);
}
touchesMovedWithEvent(touches: NSSet, event: any): void {
this.executeCallback(common.TouchAction.move, touches, event);
}
touchesEndedWithEvent(touches: NSSet, event: any): void {
this.executeCallback(common.TouchAction.up, touches, event);
}
touchesCancelledWithEvent(touches: NSSet, event: any): void {
this.executeCallback(common.TouchAction.cancel, touches, event);
}
private executeCallback(action: string, touches: NSSet, event: any): void {
if (!this._eventData) {
this._eventData = new TouchGestureEventData();
}
this._eventData.prepare(this.observer.target, action, touches, event);
this.observer._executeCallback(this._eventData);
}
}
class Pointer implements definition.Pointer {
public android: any = undefined;
public ios: UITouch = undefined;
private _view: view.View;
private _location: CGPoint;
private get location(): CGPoint {
if (!this._location) {
this._location = this.ios.locationInView(this._view._nativeView);
}
return this._location;
}
constructor(touch: UITouch, targetView: view.View) {
this.ios = touch;
this._view = targetView;
}
getX(): number {
return this.location.x;
}
getY(): number {
return this.location.x;
}
}
class TouchGestureEventData implements definition.TouchGestureEventData {
eventName: string = definition.toString(definition.GestureTypes.touch);
type: definition.GestureTypes = definition.GestureTypes.touch;
android: any = undefined;
action: string;
view: view.View;
ios: { touches: NSSet, event: { allTouches: () => NSSet } };
object: any;
private _activePointers: Array<Pointer>;
private _allPointers: Array<Pointer>;
private _mainPointer: UITouch;
public prepare(view: view.View, action: string, touches: NSSet, event: any) {
this.action = action;
this.view = view;
this.object = view;
this.ios = {
touches: touches,
event: event
};
this._mainPointer = undefined;
this._activePointers = undefined;
this._allPointers = undefined;
}
getPointerCount(): number {
return this.ios.event.allTouches().count;
}
private getMainPointer(): UITouch {
if (types.isUndefined(this._mainPointer)) {
this._mainPointer = this.ios.touches.anyObject();
}
return this._mainPointer;
}
getActivePointers(): Array<Pointer> {
if (!this._activePointers) {
this._activePointers = [];
for (let i = 0, nsArr = this.ios.touches.allObjects; i < nsArr.count; i++) {
this._activePointers.push(new Pointer(nsArr.objectAtIndex(i), this.view));
}
}
return this._activePointers;
}
getAllPointers(): Array<Pointer> {
if (!this._allPointers) {
this._allPointers = [];
let nsArr = this.ios.event.allTouches().allObjects;
for (var i = 0; i < nsArr.count; i++) {
this._allPointers.push(new Pointer(nsArr.objectAtIndex(i), this.view));
}
}
return this._allPointers;
}
getX(): number {
return this.getMainPointer().locationInView(this.view._nativeView).x;
}
getY(): number {
return this.getMainPointer().locationInView(this.view._nativeView).y
}
}