mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 21:01:34 +08:00
Optimizations
This commit is contained in:
@ -21,6 +21,8 @@ export class GesturesObserver extends common.GesturesObserver {
|
|||||||
private _panGestureDetector: CustomPanGestureDetector;
|
private _panGestureDetector: CustomPanGestureDetector;
|
||||||
private _rotateGestureDetector: CustomRotateGestureDetector;
|
private _rotateGestureDetector: CustomRotateGestureDetector;
|
||||||
|
|
||||||
|
private _eventData: TouchGestureEventData;
|
||||||
|
|
||||||
private _onTargetLoaded: (data: observable.EventData) => void;
|
private _onTargetLoaded: (data: observable.EventData) => void;
|
||||||
private _onTargetUnloaded: (data: observable.EventData) => void;
|
private _onTargetUnloaded: (data: observable.EventData) => void;
|
||||||
|
|
||||||
@ -62,12 +64,13 @@ export class GesturesObserver extends common.GesturesObserver {
|
|||||||
private _detach() {
|
private _detach() {
|
||||||
trace.write(this.target + "._detach() android:" + this.target._nativeView, "gestures");
|
trace.write(this.target + "._detach() android:" + this.target._nativeView, "gestures");
|
||||||
|
|
||||||
|
this._notifyTouch = false
|
||||||
this._simpleGestureDetector = null;
|
this._simpleGestureDetector = null;
|
||||||
this._scaleGestureDetector = null;
|
this._scaleGestureDetector = null;
|
||||||
this._swipeGestureDetector = null;
|
this._swipeGestureDetector = null;
|
||||||
this._panGestureDetector = null;
|
this._panGestureDetector = null;
|
||||||
this._rotateGestureDetector = null;
|
this._rotateGestureDetector = null;
|
||||||
this._notifyTouch = false
|
this._eventData = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _attach(target: view.View, type: definition.GestureTypes) {
|
private _attach(target: view.View, type: definition.GestureTypes) {
|
||||||
@ -101,7 +104,12 @@ export class GesturesObserver extends common.GesturesObserver {
|
|||||||
|
|
||||||
public androidOnTouchEvent(motionEvent: android.view.MotionEvent) {
|
public androidOnTouchEvent(motionEvent: android.view.MotionEvent) {
|
||||||
if (this._notifyTouch) {
|
if (this._notifyTouch) {
|
||||||
_executeCallback(this, new TouchGestureEventData(this.target, motionEvent));
|
if (!this._eventData) {
|
||||||
|
this._eventData = new TouchGestureEventData();
|
||||||
|
}
|
||||||
|
|
||||||
|
this._eventData.prepare(this.target, motionEvent);
|
||||||
|
_executeCallback(this, this._eventData);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this._simpleGestureDetector) {
|
if (this._simpleGestureDetector) {
|
||||||
@ -604,22 +612,24 @@ class Pointer implements definition.Pointer {
|
|||||||
|
|
||||||
class TouchGestureEventData implements definition.TouchGestureEventData {
|
class TouchGestureEventData implements definition.TouchGestureEventData {
|
||||||
eventName: string = definition.toString(definition.GestureTypes.touch);
|
eventName: string = definition.toString(definition.GestureTypes.touch);
|
||||||
action: string;
|
|
||||||
type: definition.GestureTypes = definition.GestureTypes.touch;
|
type: definition.GestureTypes = definition.GestureTypes.touch;
|
||||||
view: view.View;
|
|
||||||
ios: any = undefined;
|
ios: any = undefined;
|
||||||
|
action: string;
|
||||||
|
view: view.View;
|
||||||
android: android.view.MotionEvent;
|
android: android.view.MotionEvent;
|
||||||
object: any;
|
object: any;
|
||||||
|
|
||||||
private _activePointers: Array<Pointer>;
|
private _activePointers: Array<Pointer>;
|
||||||
private _allPointers: Array<Pointer>;
|
private _allPointers: Array<Pointer>;
|
||||||
|
|
||||||
constructor(view: view.View, e: android.view.MotionEvent) {
|
public prepare(view: view.View, e: android.view.MotionEvent) {
|
||||||
this.view = view;
|
this.view = view;
|
||||||
this.object = view;
|
this.object = view;
|
||||||
this.android = e;
|
this.android = e;
|
||||||
|
|
||||||
this.action = this.getActionType(e);
|
this.action = this.getActionType(e);
|
||||||
|
|
||||||
|
this._activePointers = undefined;
|
||||||
|
this._allPointers = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
getPointerCount(): number {
|
getPointerCount(): number {
|
||||||
|
7
ui/gestures/gestures.d.ts
vendored
7
ui/gestures/gestures.d.ts
vendored
@ -240,9 +240,11 @@ declare module "ui/gestures" {
|
|||||||
export class GesturesObserver {
|
export class GesturesObserver {
|
||||||
/**
|
/**
|
||||||
* Creates an instance of GesturesObserver class.
|
* Creates an instance of GesturesObserver class.
|
||||||
|
* @param target - The view for which the observer is created.
|
||||||
* @param callback - A function that will be executed when a gesture is received.
|
* @param callback - A function that will be executed when a gesture is received.
|
||||||
|
* @param context - default this argument for the callbacks.
|
||||||
*/
|
*/
|
||||||
constructor(target: view.View, callback: (args: GestureEventData) => void, thisArg: any);
|
constructor(target: view.View, callback: (args: GestureEventData) => void, context: any);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a gesture observer to a view and gesture.
|
* Registers a gesture observer to a view and gesture.
|
||||||
@ -281,8 +283,9 @@ declare module "ui/gestures" {
|
|||||||
* @param target - View which will be watched for originating a specific gesture.
|
* @param target - View which will be watched for originating a specific gesture.
|
||||||
* @param type - Type of the gesture.
|
* @param type - Type of the gesture.
|
||||||
* @param callback - A function that will be executed when a gesture is received.
|
* @param callback - A function that will be executed when a gesture is received.
|
||||||
|
* @param context - this argument for the callback.
|
||||||
*/
|
*/
|
||||||
export function observe(target: view.View, type: GestureTypes, callback: (args: GestureEventData) => void, thisArg?: any): GesturesObserver;
|
export function observe(target: view.View, type: GestureTypes, callback: (args: GestureEventData) => void, context?: any): GesturesObserver;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a string representation of a gesture type.
|
* Returns a string representation of a gesture type.
|
||||||
|
@ -342,6 +342,7 @@ function _getRotationData(args: definition.GestureEventData): definition.Rotatio
|
|||||||
|
|
||||||
class TouchGestureRecognizer extends UIGestureRecognizer {
|
class TouchGestureRecognizer extends UIGestureRecognizer {
|
||||||
public observer: GesturesObserver;
|
public observer: GesturesObserver;
|
||||||
|
private _eventData: TouchGestureEventData;
|
||||||
|
|
||||||
touchesBeganWithEvent(touches: NSSet, event: any): void {
|
touchesBeganWithEvent(touches: NSSet, event: any): void {
|
||||||
this.executeCallback(common.TouchAction.down, touches, event);
|
this.executeCallback(common.TouchAction.down, touches, event);
|
||||||
@ -360,8 +361,12 @@ class TouchGestureRecognizer extends UIGestureRecognizer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private executeCallback(action: string, touches: NSSet, event: any): void {
|
private executeCallback(action: string, touches: NSSet, event: any): void {
|
||||||
var args = new TouchGestureEventData(this.observer.target, action, touches, event);
|
if (!this._eventData) {
|
||||||
this.observer._executeCallback(args);
|
this._eventData = new TouchGestureEventData();
|
||||||
|
}
|
||||||
|
|
||||||
|
this._eventData.prepare(this.observer.target, action, touches, event);
|
||||||
|
this.observer._executeCallback(this._eventData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -396,43 +401,47 @@ class Pointer implements definition.Pointer {
|
|||||||
|
|
||||||
class TouchGestureEventData implements definition.TouchGestureEventData {
|
class TouchGestureEventData implements definition.TouchGestureEventData {
|
||||||
eventName: string = definition.toString(definition.GestureTypes.touch);
|
eventName: string = definition.toString(definition.GestureTypes.touch);
|
||||||
action: string;
|
|
||||||
type: definition.GestureTypes = definition.GestureTypes.touch;
|
type: definition.GestureTypes = definition.GestureTypes.touch;
|
||||||
|
android: any = undefined;
|
||||||
|
action: string;
|
||||||
view: view.View;
|
view: view.View;
|
||||||
ios: { touches: NSSet, event: { allTouches: () => NSSet } };
|
ios: { touches: NSSet, event: { allTouches: () => NSSet } };
|
||||||
android: any = undefined;
|
|
||||||
object: any;
|
object: any;
|
||||||
private _allPinters: Array<Pointer>;
|
|
||||||
private _activePointers: Array<Pointer>;
|
private _activePointers: Array<Pointer>;
|
||||||
|
private _allPointers: Array<Pointer>;
|
||||||
private _mainPointer: UITouch;
|
private _mainPointer: UITouch;
|
||||||
private get mainPointer(): UITouch {
|
|
||||||
if (types.isUndefined(this._mainPointer)) {
|
|
||||||
this._mainPointer = this.ios.touches.anyObject();
|
|
||||||
}
|
|
||||||
return this._mainPointer;
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor(view: view.View, action: string, touches: NSSet, event: any) {
|
public prepare(view: view.View, action: string, touches: NSSet, event: any) {
|
||||||
|
this.action = action;
|
||||||
this.view = view;
|
this.view = view;
|
||||||
this.object = view;
|
this.object = view;
|
||||||
this.action = action;
|
|
||||||
this.ios = {
|
this.ios = {
|
||||||
touches: touches,
|
touches: touches,
|
||||||
event: event
|
event: event
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this._mainPointer = undefined;
|
||||||
|
this._activePointers = undefined;
|
||||||
|
this._allPointers = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
getPointerCount(): number {
|
getPointerCount(): number {
|
||||||
return this.ios.event.allTouches().count;
|
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> {
|
getActivePointers(): Array<Pointer> {
|
||||||
if (!this._activePointers) {
|
if (!this._activePointers) {
|
||||||
this._activePointers = [];
|
this._activePointers = [];
|
||||||
|
|
||||||
let nsArr = this.ios.touches.allObjects;
|
for (let i = 0, nsArr = this.ios.touches.allObjects; i < nsArr.count; i++) {
|
||||||
for (var i = 0; i < nsArr.count; i++) {
|
|
||||||
this._activePointers.push(new Pointer(nsArr.objectAtIndex(i), this.view));
|
this._activePointers.push(new Pointer(nsArr.objectAtIndex(i), this.view));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -441,23 +450,23 @@ class TouchGestureEventData implements definition.TouchGestureEventData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getAllPointers(): Array<Pointer> {
|
getAllPointers(): Array<Pointer> {
|
||||||
if (!this._allPinters) {
|
if (!this._allPointers) {
|
||||||
this._allPinters = [];
|
this._allPointers = [];
|
||||||
|
|
||||||
let nsArr = this.ios.event.allTouches().allObjects;
|
let nsArr = this.ios.event.allTouches().allObjects;
|
||||||
for (var i = 0; i < nsArr.count; i++) {
|
for (var i = 0; i < nsArr.count; i++) {
|
||||||
this._allPinters.push(new Pointer(nsArr.objectAtIndex(i), this.view));
|
this._allPointers.push(new Pointer(nsArr.objectAtIndex(i), this.view));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._allPinters;
|
return this._allPointers;
|
||||||
}
|
}
|
||||||
|
|
||||||
getX(): number {
|
getX(): number {
|
||||||
return this.mainPointer.locationInView(this.view._nativeView).x;
|
return this.getMainPointer().locationInView(this.view._nativeView).x;
|
||||||
}
|
}
|
||||||
|
|
||||||
getY(): number {
|
getY(): number {
|
||||||
return this.mainPointer.locationInView(this.view._nativeView).y
|
return this.getMainPointer().locationInView(this.view._nativeView).y
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user