mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-18 22:01:42 +08:00
feat(gestures): GestureEvents.gestureAttached added to modify native recognizers when needed
This commit is contained in:
@ -1,6 +1,10 @@
|
||||
import { GestureEventData, GesturesObserver as GesturesObserverDefinition } from '.';
|
||||
import { View } from '../core/view';
|
||||
|
||||
export enum GestureEvents {
|
||||
gestureAttached = 'gestureAttached',
|
||||
}
|
||||
|
||||
export enum GestureTypes {
|
||||
tap = 1 << 0,
|
||||
doubleTap = 1 << 1,
|
||||
|
@ -4,7 +4,7 @@ import { View } from '../core/view';
|
||||
import { EventData } from '../../data/observable';
|
||||
|
||||
// Types.
|
||||
import { GesturesObserverBase, toString, TouchAction, GestureStateTypes, GestureTypes, SwipeDirection } from './gestures-common';
|
||||
import { GesturesObserverBase, toString, TouchAction, GestureStateTypes, GestureTypes, SwipeDirection, GestureEvents } from './gestures-common';
|
||||
|
||||
// Import layout from utils directly to avoid circular references
|
||||
import { layout } from '../../utils';
|
||||
@ -297,31 +297,41 @@ export class GesturesObserver extends GesturesObserverBase {
|
||||
private _attach(target: View, type: GestureTypes) {
|
||||
this._detach();
|
||||
|
||||
let recognizer;
|
||||
|
||||
if (type & GestureTypes.tap || type & GestureTypes.doubleTap || type & GestureTypes.longPress) {
|
||||
initializeTapAndDoubleTapGestureListener();
|
||||
this._simpleGestureDetector = <any>new androidx.core.view.GestureDetectorCompat(target._context, new TapAndDoubleTapGestureListener(this, this.target, type));
|
||||
recognizer = this._simpleGestureDetector = <any>new androidx.core.view.GestureDetectorCompat(target._context, new TapAndDoubleTapGestureListener(this, this.target, type));
|
||||
}
|
||||
|
||||
if (type & GestureTypes.pinch) {
|
||||
initializePinchGestureListener();
|
||||
this._scaleGestureDetector = new android.view.ScaleGestureDetector(target._context, new PinchGestureListener(this, this.target));
|
||||
recognizer = this._scaleGestureDetector = new android.view.ScaleGestureDetector(target._context, new PinchGestureListener(this, this.target));
|
||||
}
|
||||
|
||||
if (type & GestureTypes.swipe) {
|
||||
initializeSwipeGestureListener();
|
||||
this._swipeGestureDetector = <any>new androidx.core.view.GestureDetectorCompat(target._context, new SwipeGestureListener(this, this.target));
|
||||
recognizer = this._swipeGestureDetector = <any>new androidx.core.view.GestureDetectorCompat(target._context, new SwipeGestureListener(this, this.target));
|
||||
}
|
||||
|
||||
if (type & GestureTypes.pan) {
|
||||
this._panGestureDetector = new CustomPanGestureDetector(this, this.target);
|
||||
recognizer = this._panGestureDetector = new CustomPanGestureDetector(this, this.target);
|
||||
}
|
||||
|
||||
if (type & GestureTypes.rotation) {
|
||||
this._rotateGestureDetector = new CustomRotateGestureDetector(this, this.target);
|
||||
recognizer = this._rotateGestureDetector = new CustomRotateGestureDetector(this, this.target);
|
||||
}
|
||||
|
||||
if (type & GestureTypes.touch) {
|
||||
this._notifyTouch = true;
|
||||
} else {
|
||||
this.target.notify({
|
||||
eventName: GestureEvents.gestureAttached,
|
||||
object: this.target,
|
||||
type,
|
||||
view: this.target,
|
||||
ios: recognizer,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
11
packages/core/ui/gestures/index.d.ts
vendored
11
packages/core/ui/gestures/index.d.ts
vendored
@ -1,6 +1,17 @@
|
||||
import { View } from '../core/view';
|
||||
import { EventData } from '../../data/observable';
|
||||
|
||||
/**
|
||||
* Events emitted during gesture lifecycle
|
||||
*/
|
||||
export enum GestureEvents {
|
||||
/**
|
||||
* When the gesture is attached to the view
|
||||
* Provides access to the native gesture recognizer for further customization
|
||||
*/
|
||||
gestureAttached = 'gestureAttached',
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines an enum with supported gesture types.
|
||||
*/
|
||||
|
@ -5,7 +5,7 @@ import { View } from '../core/view';
|
||||
import { EventData } from '../../data/observable';
|
||||
|
||||
// Types.
|
||||
import { GesturesObserverBase, toString, TouchAction, GestureStateTypes, GestureTypes, SwipeDirection } from './gestures-common';
|
||||
import { GesturesObserverBase, toString, TouchAction, GestureStateTypes, GestureTypes, SwipeDirection, GestureEvents } from './gestures-common';
|
||||
|
||||
// Import layout from utils directly to avoid circular references
|
||||
import { layout } from '../../utils';
|
||||
@ -306,6 +306,14 @@ export class GesturesObserver extends GesturesObserverBase {
|
||||
target: target,
|
||||
};
|
||||
}
|
||||
|
||||
this.target.notify({
|
||||
eventName: GestureEvents.gestureAttached,
|
||||
object: this.target,
|
||||
type,
|
||||
view: this.target,
|
||||
ios: recognizer,
|
||||
});
|
||||
}
|
||||
|
||||
return recognizer;
|
||||
@ -348,7 +356,7 @@ function _getUIGestureRecognizerType(type: GestureTypes): any {
|
||||
function getState(recognizer: UIGestureRecognizer) {
|
||||
if (recognizer.state === UIGestureRecognizerState.Began) {
|
||||
return GestureStateTypes.began;
|
||||
} else if (recognizer.state === UIGestureRecognizerState.Cancelled) {
|
||||
} else if (recognizer.state === UIGestureRecognizerState.Cancelled || recognizer.state === UIGestureRecognizerState.Failed) {
|
||||
return GestureStateTypes.cancelled;
|
||||
} else if (recognizer.state === UIGestureRecognizerState.Changed) {
|
||||
return GestureStateTypes.changed;
|
||||
|
@ -27,7 +27,7 @@ export * from './editable-text-base';
|
||||
export { Frame, setActivityCallbacks } from './frame';
|
||||
export type { NavigationEntry, NavigationContext, NavigationTransition, BackstackEntry, ViewEntry, AndroidActivityCallbacks } from './frame';
|
||||
|
||||
export { GesturesObserver, TouchAction, GestureTypes, GestureStateTypes, SwipeDirection } from './gestures';
|
||||
export { GesturesObserver, TouchAction, GestureTypes, GestureStateTypes, SwipeDirection, GestureEvents } from './gestures';
|
||||
export type { GestureEventData, GestureEventDataWithState, TapGestureEventData, PanGestureEventData, PinchGestureEventData, RotationGestureEventData, SwipeGestureEventData, TouchGestureEventData } from './gestures';
|
||||
|
||||
export { HtmlView } from './html-view';
|
||||
|
Reference in New Issue
Block a user