UNPKG

18.4 kBJavaScriptView Raw
1// Definitions.
2// Types.
3import { GesturesObserverBase, toString, TouchAction, GestureStateTypes, GestureTypes, SwipeDirection, GestureEvents } from './gestures-common';
4export * from './gestures-common';
5export function observe(target, type, callback, context) {
6 const observer = new GesturesObserver(target, callback, context);
7 observer.observe(type);
8 return observer;
9}
10var UIGestureRecognizerDelegateImpl = /** @class */ (function (_super) {
11 __extends(UIGestureRecognizerDelegateImpl, _super);
12 function UIGestureRecognizerDelegateImpl() {
13 return _super !== null && _super.apply(this, arguments) || this;
14 }
15 UIGestureRecognizerDelegateImpl.prototype.gestureRecognizerShouldRecognizeSimultaneouslyWithGestureRecognizer = function (gestureRecognizer, otherGestureRecognizer) {
16 return true;
17 };
18 UIGestureRecognizerDelegateImpl.prototype.gestureRecognizerShouldRequireFailureOfGestureRecognizer = function (gestureRecognizer, otherGestureRecognizer) {
19 // If both gesture recognizers are of type UITapGestureRecognizer & one of them is a doubleTap,
20 // we must require a failure.
21 if (gestureRecognizer instanceof UITapGestureRecognizer && otherGestureRecognizer instanceof UITapGestureRecognizer && otherGestureRecognizer.numberOfTapsRequired === 2) {
22 return true;
23 }
24 return false;
25 };
26 UIGestureRecognizerDelegateImpl.ObjCProtocols = [UIGestureRecognizerDelegate];
27 return UIGestureRecognizerDelegateImpl;
28}(NSObject));
29const recognizerDelegateInstance = UIGestureRecognizerDelegateImpl.new();
30var UIGestureRecognizerImpl = /** @class */ (function (_super) {
31 __extends(UIGestureRecognizerImpl, _super);
32 function UIGestureRecognizerImpl() {
33 return _super !== null && _super.apply(this, arguments) || this;
34 }
35 UIGestureRecognizerImpl.initWithOwnerTypeCallback = function (owner, type, callback, thisArg) {
36 var handler = UIGestureRecognizerImpl.new();
37 handler._owner = owner;
38 handler._type = type;
39 if (callback) {
40 handler._callback = callback;
41 }
42 if (thisArg) {
43 handler._context = thisArg;
44 }
45 return handler;
46 };
47 UIGestureRecognizerImpl.prototype.recognize = function (recognizer) {
48 var _a;
49 var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.deref();
50 var callback = this._callback ? this._callback : owner ? owner.callback : null;
51 var typeParam = this._type;
52 var target = owner ? owner.target : undefined;
53 var args = {
54 type: typeParam,
55 view: target,
56 ios: recognizer,
57 android: undefined,
58 object: target,
59 eventName: toString(typeParam),
60 };
61 if (callback) {
62 callback.call(this._context, args);
63 }
64 };
65 UIGestureRecognizerImpl.ObjCExposedMethods = {
66 recognize: {
67 returns: interop.types.void,
68 params: [UIGestureRecognizer],
69 },
70 };
71 return UIGestureRecognizerImpl;
72}(NSObject));
73export class GesturesObserver extends GesturesObserverBase {
74 constructor(target, callback, context) {
75 super(target, callback, context);
76 this._recognizers = {};
77 }
78 androidOnTouchEvent(motionEvent) {
79 //
80 }
81 observe(type) {
82 if (this.target) {
83 this.type = type;
84 this._onTargetLoaded = (args) => {
85 this._attach(this.target, type);
86 };
87 this._onTargetUnloaded = (args) => {
88 this._detach();
89 };
90 this.target.on('loaded', this._onTargetLoaded);
91 this.target.on('unloaded', this._onTargetUnloaded);
92 if (this.target.isLoaded) {
93 this._attach(this.target, type);
94 }
95 }
96 }
97 _attach(target, type) {
98 this._detach();
99 if (target && target.nativeViewProtected && target.nativeViewProtected.addGestureRecognizer) {
100 const nativeView = target.nativeViewProtected;
101 if (type & GestureTypes.tap) {
102 nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.tap, (args) => {
103 if (args.view) {
104 this._executeCallback(_getTapData(args));
105 }
106 }));
107 }
108 if (type & GestureTypes.doubleTap) {
109 nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.doubleTap, (args) => {
110 if (args.view) {
111 this._executeCallback(_getTapData(args));
112 }
113 }));
114 }
115 if (type & GestureTypes.pinch) {
116 nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.pinch, (args) => {
117 if (args.view) {
118 this._executeCallback(_getPinchData(args));
119 }
120 }));
121 }
122 if (type & GestureTypes.pan) {
123 nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.pan, (args) => {
124 if (args.view) {
125 this._executeCallback(_getPanData(args, target.nativeViewProtected));
126 }
127 }));
128 }
129 if (type & GestureTypes.swipe) {
130 nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.swipe, (args) => {
131 if (args.view) {
132 this._executeCallback(_getSwipeData(args));
133 }
134 }, 8 /* UISwipeGestureRecognizerDirection.Down */));
135 nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.swipe, (args) => {
136 if (args.view) {
137 this._executeCallback(_getSwipeData(args));
138 }
139 }, 2 /* UISwipeGestureRecognizerDirection.Left */));
140 nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.swipe, (args) => {
141 if (args.view) {
142 this._executeCallback(_getSwipeData(args));
143 }
144 }, 1 /* UISwipeGestureRecognizerDirection.Right */));
145 nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.swipe, (args) => {
146 if (args.view) {
147 this._executeCallback(_getSwipeData(args));
148 }
149 }, 4 /* UISwipeGestureRecognizerDirection.Up */));
150 }
151 if (type & GestureTypes.rotation) {
152 nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.rotation, (args) => {
153 if (args.view) {
154 this._executeCallback(_getRotationData(args));
155 }
156 }));
157 }
158 if (type & GestureTypes.longPress) {
159 nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.longPress, (args) => {
160 if (args.view) {
161 this._executeCallback(_getLongPressData(args));
162 }
163 }));
164 }
165 if (type & GestureTypes.touch) {
166 nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.touch));
167 }
168 }
169 }
170 _detach() {
171 if (this.target && this.target.nativeViewProtected) {
172 for (const name in this._recognizers) {
173 if (this._recognizers.hasOwnProperty(name)) {
174 const item = this._recognizers[name];
175 this.target.nativeViewProtected.removeGestureRecognizer(item.recognizer);
176 item.recognizer = null;
177 item.target = null;
178 }
179 }
180 this._recognizers = {};
181 }
182 }
183 disconnect() {
184 this._detach();
185 if (this.target) {
186 this.target.off('loaded', this._onTargetLoaded);
187 this.target.off('unloaded', this._onTargetUnloaded);
188 this._onTargetLoaded = null;
189 this._onTargetUnloaded = null;
190 }
191 // clears target, context and callback references
192 super.disconnect();
193 }
194 _executeCallback(args) {
195 if (this.callback) {
196 this.callback.call(this.context, args);
197 }
198 }
199 _createRecognizer(type, callback, swipeDirection) {
200 let recognizer;
201 let name = toString(type);
202 const target = _createUIGestureRecognizerTarget(this, type, callback, this.context);
203 const recognizerType = _getUIGestureRecognizerType(type);
204 if (recognizerType) {
205 recognizer = recognizerType.alloc().initWithTargetAction(target, 'recognize');
206 if (type === GestureTypes.swipe && swipeDirection) {
207 name = name + swipeDirection.toString();
208 recognizer.direction = swipeDirection;
209 }
210 else if (type === GestureTypes.touch) {
211 recognizer.observer = this;
212 }
213 else if (type === GestureTypes.doubleTap) {
214 recognizer.numberOfTapsRequired = 2;
215 }
216 if (recognizer) {
217 recognizer.delegate = recognizerDelegateInstance;
218 this._recognizers[name] = {
219 recognizer: recognizer,
220 target: target,
221 };
222 }
223 this.target.notify({
224 eventName: GestureEvents.gestureAttached,
225 object: this.target,
226 type,
227 view: this.target,
228 ios: recognizer,
229 });
230 }
231 return recognizer;
232 }
233}
234function _createUIGestureRecognizerTarget(owner, type, callback, context) {
235 return UIGestureRecognizerImpl.initWithOwnerTypeCallback(new WeakRef(owner), type, callback, context);
236}
237function _getUIGestureRecognizerType(type) {
238 let nativeType = null;
239 if (type === GestureTypes.tap) {
240 nativeType = UITapGestureRecognizer;
241 }
242 else if (type === GestureTypes.doubleTap) {
243 nativeType = UITapGestureRecognizer;
244 }
245 else if (type === GestureTypes.pinch) {
246 nativeType = UIPinchGestureRecognizer;
247 }
248 else if (type === GestureTypes.pan) {
249 nativeType = UIPanGestureRecognizer;
250 }
251 else if (type === GestureTypes.swipe) {
252 nativeType = UISwipeGestureRecognizer;
253 }
254 else if (type === GestureTypes.rotation) {
255 nativeType = UIRotationGestureRecognizer;
256 }
257 else if (type === GestureTypes.longPress) {
258 nativeType = UILongPressGestureRecognizer;
259 }
260 else if (type === GestureTypes.touch) {
261 nativeType = TouchGestureRecognizer;
262 }
263 return nativeType;
264}
265function getState(recognizer) {
266 if (recognizer.state === 1 /* UIGestureRecognizerState.Began */) {
267 return GestureStateTypes.began;
268 }
269 else if (recognizer.state === 4 /* UIGestureRecognizerState.Cancelled */ || recognizer.state === 5 /* UIGestureRecognizerState.Failed */) {
270 return GestureStateTypes.cancelled;
271 }
272 else if (recognizer.state === 2 /* UIGestureRecognizerState.Changed */) {
273 return GestureStateTypes.changed;
274 }
275 else if (recognizer.state === 3 /* UIGestureRecognizerState.Ended */) {
276 return GestureStateTypes.ended;
277 }
278}
279function _getSwipeDirection(direction) {
280 if (direction === 8 /* UISwipeGestureRecognizerDirection.Down */) {
281 return SwipeDirection.down;
282 }
283 else if (direction === 2 /* UISwipeGestureRecognizerDirection.Left */) {
284 return SwipeDirection.left;
285 }
286 else if (direction === 1 /* UISwipeGestureRecognizerDirection.Right */) {
287 return SwipeDirection.right;
288 }
289 else if (direction === 4 /* UISwipeGestureRecognizerDirection.Up */) {
290 return SwipeDirection.up;
291 }
292}
293function _getTapData(args) {
294 const recognizer = args.ios;
295 const center = recognizer.locationInView(args.view.nativeViewProtected);
296 return {
297 type: args.type,
298 view: args.view,
299 ios: args.ios,
300 android: undefined,
301 eventName: args.eventName,
302 object: args.object,
303 getPointerCount: () => recognizer.numberOfTouches,
304 getX: () => center.x,
305 getY: () => center.y,
306 };
307}
308function _getPinchData(args) {
309 const recognizer = args.ios;
310 const center = recognizer.locationInView(args.view.nativeViewProtected);
311 return {
312 type: args.type,
313 view: args.view,
314 ios: args.ios,
315 android: undefined,
316 scale: recognizer.scale,
317 getFocusX: () => center.x,
318 getFocusY: () => center.y,
319 object: args.view,
320 eventName: toString(args.type),
321 state: getState(recognizer),
322 };
323}
324function _getSwipeData(args) {
325 const recognizer = args.ios;
326 return {
327 type: args.type,
328 view: args.view,
329 ios: args.ios,
330 android: undefined,
331 direction: _getSwipeDirection(recognizer.direction),
332 object: args.view,
333 eventName: toString(args.type),
334 };
335}
336function _getPanData(args, view) {
337 const recognizer = args.ios;
338 return {
339 type: args.type,
340 view: args.view,
341 ios: args.ios,
342 android: undefined,
343 deltaX: recognizer.translationInView(view).x,
344 deltaY: recognizer.translationInView(view).y,
345 object: args.view,
346 eventName: toString(args.type),
347 state: getState(recognizer),
348 };
349}
350function _getRotationData(args) {
351 const recognizer = args.ios;
352 return {
353 type: args.type,
354 view: args.view,
355 ios: args.ios,
356 android: undefined,
357 rotation: recognizer.rotation * (180.0 / Math.PI),
358 object: args.view,
359 eventName: toString(args.type),
360 state: getState(recognizer),
361 };
362}
363function _getLongPressData(args) {
364 const recognizer = args.ios;
365 return {
366 type: args.type,
367 view: args.view,
368 ios: args.ios,
369 android: undefined,
370 object: args.view,
371 eventName: toString(args.type),
372 state: getState(recognizer),
373 };
374}
375var TouchGestureRecognizer = /** @class */ (function (_super) {
376 __extends(TouchGestureRecognizer, _super);
377 function TouchGestureRecognizer() {
378 return _super !== null && _super.apply(this, arguments) || this;
379 }
380 TouchGestureRecognizer.prototype.touchesBeganWithEvent = function (touches, event) {
381 this.executeCallback(TouchAction.down, touches, event);
382 if (this.view) {
383 this.view.touchesBeganWithEvent(touches, event);
384 }
385 };
386 TouchGestureRecognizer.prototype.touchesMovedWithEvent = function (touches, event) {
387 this.executeCallback(TouchAction.move, touches, event);
388 if (this.view) {
389 this.view.touchesMovedWithEvent(touches, event);
390 }
391 };
392 TouchGestureRecognizer.prototype.touchesEndedWithEvent = function (touches, event) {
393 this.executeCallback(TouchAction.up, touches, event);
394 if (this.view) {
395 this.view.touchesEndedWithEvent(touches, event);
396 }
397 };
398 TouchGestureRecognizer.prototype.touchesCancelledWithEvent = function (touches, event) {
399 this.executeCallback(TouchAction.cancel, touches, event);
400 if (this.view) {
401 this.view.touchesCancelledWithEvent(touches, event);
402 }
403 };
404 TouchGestureRecognizer.prototype.executeCallback = function (action, touches, event) {
405 if (!this._eventData) {
406 this._eventData = new TouchGestureEventData();
407 }
408 this._eventData.prepare(this.observer.target, action, touches, event);
409 this.observer._executeCallback(this._eventData);
410 };
411 return TouchGestureRecognizer;
412}(UIGestureRecognizer));
413class Pointer {
414 get location() {
415 if (!this._location) {
416 this._location = this.ios.locationInView(this._view.nativeViewProtected);
417 }
418 return this._location;
419 }
420 constructor(touch, targetView) {
421 this.android = undefined;
422 this.ios = undefined;
423 this.ios = touch;
424 this._view = targetView;
425 }
426 getX() {
427 return this.location.x;
428 }
429 getY() {
430 return this.location.y;
431 }
432}
433class TouchGestureEventData {
434 constructor() {
435 this.eventName = toString(GestureTypes.touch);
436 this.type = GestureTypes.touch;
437 this.android = undefined;
438 }
439 prepare(view, action, touches, event) {
440 this.action = action;
441 this.view = view;
442 this.object = view;
443 this.ios = {
444 touches: touches,
445 event: event,
446 };
447 this._mainPointer = undefined;
448 this._activePointers = undefined;
449 this._allPointers = undefined;
450 }
451 getPointerCount() {
452 return this.ios.event.allTouches.count;
453 }
454 getMainPointer() {
455 if (this._mainPointer === undefined) {
456 this._mainPointer = this.ios.touches.anyObject();
457 }
458 return this._mainPointer;
459 }
460 getActivePointers() {
461 if (!this._activePointers) {
462 this._activePointers = [];
463 for (let i = 0, nsArr = this.ios.touches.allObjects; i < nsArr.count; i++) {
464 this._activePointers.push(new Pointer(nsArr.objectAtIndex(i), this.view));
465 }
466 }
467 return this._activePointers;
468 }
469 getAllPointers() {
470 if (!this._allPointers) {
471 this._allPointers = [];
472 const nsArr = this.ios.event.allTouches.allObjects;
473 for (let i = 0; i < nsArr.count; i++) {
474 this._allPointers.push(new Pointer(nsArr.objectAtIndex(i), this.view));
475 }
476 }
477 return this._allPointers;
478 }
479 getX() {
480 const offset = this.view.nativeViewProtected.contentOffset;
481 const offsetX = offset ? offset.x : 0;
482 return this.getMainPointer().locationInView(this.view.nativeViewProtected).x - offsetX;
483 }
484 getY() {
485 const offset = this.view.nativeViewProtected.contentOffset;
486 const offsetY = offset ? offset.y : 0;
487 return this.getMainPointer().locationInView(this.view.nativeViewProtected).y - offsetY;
488 }
489}
490//# sourceMappingURL=index.ios.js.map
\No newline at end of file