mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Merge pull request #277 from NativeScript/feature/gestures-fix
Gestures rework in android
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
import definition = require("ui/gestures");
|
import definition = require("ui/gestures");
|
||||||
import observable = require("data/observable");
|
import observable = require("data/observable");
|
||||||
import view = require("ui/core/view");
|
import view = require("ui/core/view");
|
||||||
|
import trace = require("trace");
|
||||||
|
|
||||||
// merge the exports of the request file with the exports of this file
|
// merge the exports of the request file with the exports of this file
|
||||||
declare var exports;
|
declare var exports;
|
||||||
@@ -9,7 +10,6 @@ require("utils/module-merge").merge(common, exports);
|
|||||||
|
|
||||||
var SWIPE_THRESHOLD = 100;
|
var SWIPE_THRESHOLD = 100;
|
||||||
var SWIPE_VELOCITY_THRESHOLD = 100;
|
var SWIPE_VELOCITY_THRESHOLD = 100;
|
||||||
var LOADED = "loaded";
|
|
||||||
|
|
||||||
export class GesturesObserver implements definition.GesturesObserver {
|
export class GesturesObserver implements definition.GesturesObserver {
|
||||||
private _callback: (args: definition.GestureEventData) => void;
|
private _callback: (args: definition.GestureEventData) => void;
|
||||||
@@ -22,6 +22,7 @@ export class GesturesObserver implements definition.GesturesObserver {
|
|||||||
public _panGestureDetector: android.view.GestureDetector
|
public _panGestureDetector: android.view.GestureDetector
|
||||||
|
|
||||||
private _onTargetLoaded: (data: observable.EventData) => void;
|
private _onTargetLoaded: (data: observable.EventData) => void;
|
||||||
|
private _onTargetUnloaded: (data: observable.EventData) => void;
|
||||||
|
|
||||||
constructor(callback: (args: definition.GestureEventData) => void) {
|
constructor(callback: (args: definition.GestureEventData) => void) {
|
||||||
this._callback = callback;
|
this._callback = callback;
|
||||||
@@ -33,26 +34,42 @@ export class GesturesObserver implements definition.GesturesObserver {
|
|||||||
|
|
||||||
public observe(target: view.View, type: definition.GestureTypes) {
|
public observe(target: view.View, type: definition.GestureTypes) {
|
||||||
if (target) {
|
if (target) {
|
||||||
|
this._target = target;
|
||||||
|
this._onTargetLoaded = args => {
|
||||||
|
trace.write(this._target + ".target loaded. android:" + this._target.android, "gestures");
|
||||||
|
this._attach(target, type);
|
||||||
|
};
|
||||||
|
this._onTargetUnloaded = args => {
|
||||||
|
trace.write(this._target + ".target unloaded. android:" + this._target.android, "gestures");
|
||||||
|
this._dettach();
|
||||||
|
};
|
||||||
|
|
||||||
|
target.on(view.knownEvents.loaded, this._onTargetLoaded);
|
||||||
|
target.on(view.knownEvents.unloaded, this._onTargetUnloaded);
|
||||||
|
|
||||||
if (target.isLoaded) {
|
if (target.isLoaded) {
|
||||||
this._attach(target, type);
|
this._attach(target, type);
|
||||||
} else {
|
|
||||||
// TODO: Memory leak here - cyclic reference through closure
|
|
||||||
this._onTargetLoaded = args => { this._attach(target, type); };
|
|
||||||
target.on(LOADED, this._onTargetLoaded);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnect() {
|
public disconnect() {
|
||||||
if (this._target) {
|
this._dettach();
|
||||||
if (this._target.android) {
|
|
||||||
this._target.android.setOnTouchListener(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this._onTargetLoaded != null) {
|
if (this._target) {
|
||||||
this._target.off(LOADED, this._onTargetLoaded);
|
this._target.off(view.knownEvents.loaded, this._onTargetLoaded);
|
||||||
this._onTargetLoaded = null;
|
this._target.off(view.knownEvents.unloaded, this._onTargetUnloaded);
|
||||||
}
|
|
||||||
|
this._onTargetLoaded = null;
|
||||||
|
this._onTargetUnloaded = null;
|
||||||
|
this._target = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private _dettach() {
|
||||||
|
trace.write(this._target + "._detach() android:" + this._target.android, "gestures");
|
||||||
|
if (this._target && this._target.android) {
|
||||||
|
this._target.android.setOnTouchListener(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
this._onTouchListener = null;
|
this._onTouchListener = null;
|
||||||
@@ -60,14 +77,11 @@ export class GesturesObserver implements definition.GesturesObserver {
|
|||||||
this._scaleGestureDetector = null;
|
this._scaleGestureDetector = null;
|
||||||
this._swipeGestureDetector = null;
|
this._swipeGestureDetector = null;
|
||||||
this._panGestureDetector = null;
|
this._panGestureDetector = null;
|
||||||
|
|
||||||
this._target = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _attach(target: view.View, type: definition.GestureTypes) {
|
private _attach(target: view.View, type: definition.GestureTypes) {
|
||||||
this.disconnect();
|
trace.write(this._target + "._attach() android:" + this._target.android, "gestures");
|
||||||
|
this._dettach();
|
||||||
this._target = target;
|
|
||||||
|
|
||||||
if (type & definition.GestureTypes.Tap || type & definition.GestureTypes.DoubleTap || type & definition.GestureTypes.LongPress) {
|
if (type & definition.GestureTypes.Tap || type & definition.GestureTypes.DoubleTap || type & definition.GestureTypes.LongPress) {
|
||||||
this._simpleGestureDetector = new android.support.v4.view.GestureDetectorCompat(target._context, new TapAndDoubleTapGestureListener(this, this._target));
|
this._simpleGestureDetector = new android.support.v4.view.GestureDetectorCompat(target._context, new TapAndDoubleTapGestureListener(this, this._target));
|
||||||
|
|||||||
Reference in New Issue
Block a user