nav-title | title | description | position |
---|---|---|---|
Gestures in NativeScript |
UI: Gestures |
Learn what are the touch gestures that NativeScript supports and how to make use of them. |
16 |
Gestures, such as tap, slide, and pinch, allow users to interact with your app by manipulating UI elements on the screen.
In NativeScript, View
—the base class for all NativeScript UI elements—has an observe
method that lets you subscribe to gestures recognized by the UI element.
As first parameter of the observe
method you pass the type of gesture you are interested in. The second parameter is a function that is called each time the specified gesture is recognized. The function arguments contain additional information about the gesture, if applicable. Calling the observe
method returns a GestureObserver
object that you will need later to stop detecting gestures.
- observe( type Number, callback Function... ) GesturesObserver
- type - Number
- callback - Function(args GestureEventData)
- return - GesturesObserver
The next sections introduce you to all the gestures recognized by NativeScript:
To learn how to stop receiving information about gestures, go to Stop Detecting Gestures.
Triggers the default functionality for a given item.
Action: Briefly touch the screen
var label = new labelModule.Label();
var observer = label.observe(gestures.GestureTypes.Tap, function (args) {
console.log("Tap");
});
var label = new labelModule.Label();
var observer = label.observe(gestures.GestureTypes.Tap, function (args: gestures.GestureEventData) {
console.log("Tap");
});
Scales up the object with a predefined percentage, centered around the double-tapped region. Keeping repeating the gesture continues to scale up until the maximum scale is reached.
In nested views, the gesture scales up the smallest targetable view or returns it to its original scale.
Also used for text selection.
Action: Two taps in a quick succession
var label = new labelModule.Label();
var observer = label.observe(gestures.GestureTypes.DoubleTap, function (args) {
console.log("Double Tap");
});
var label = new labelModule.Label();
var observer = label.observe(gestures.GestureTypes.DoubleTap, function (args: gestures.GestureEventData) {
console.log("Double Tap");
});
Enters data selection mode. Allows you to select one or more items in a view and act upon the data using a contextual action bar. Avoid using long press for displaying contextual menus.
Action: Press you finger against the screen for a few moments
var label = new labelModule.Label();
var observer = label.observe(gestures.GestureTypes.LongPress, function (args) {
console.log("Long Press");
});
var label = new labelModule.Label();
var observer = label.observe(gestures.GestureTypes.LongPress, function (args: gestures.GestureEventData) {
console.log("Long Press");
});
Navigates between views in the same hierarchy. Swipes are quick and affect the screen even after the finger is lifted off the screen.
Action: Swiftly slide your finger across the screen
var label = new labelModule.Label();
var observer = label.observe(gestures.GestureTypes.Swipe, function (args) {
console.log("Swipe Direction: " + args.direction);
});
var label = new labelModule.Label();
var observer = label.observe(gestures.GestureTypes.Swipe, function (args: gestures.SwipeGestureEventData) {
console.log("Swipe Direction: " + args.direction);
});
Scrolls overflowing content. Pans are executed more slowly and allow for more precision and the screen stops responding as soon as the finger is lifted off it.
Action: Press your finger down and immediately start moving it around
var label = new labelModule.Label();
var observer = label.observe(gestures.GestureTypes.Pan, function (args) {
console.log("Pan deltaX:" + args.deltaX + "; deltaY:" + args.deltaY + ";");
});
var observer = label.observe(gestures.GestureTypes.Pan, function (args: gestures.PanGestureEventData) {
console.log("Pan deltaX:" + args.deltaX + "; deltaY:" + args.deltaY + ";");
});
Zooms into content or out of content.
Action 1: Touch using two of your fingers, then move them towards each other Action 2: Touch using two of your fingers, then move them away of each other
var label = new labelModule.Label();
var observer = label.observe(gestures.GestureTypes.Pinch, function (args) {
console.log("Pinch Scale: " + args.scale);
});
var label = new labelModule.Label();
var observer = label.observe(gestures.GestureTypes.Pinch, function (args: gestures.PinchGestureEventData) {
console.log("Pinch Scale: " + args.scale);
});
Rotates content clockwise or counterclockwise.
Action: Touch using two of your fingers, then rotate them simultaneously left or right
var label = new labelModule.Label();
var observer = label.observe(gestures.GestureTypes.Rotation, function (args) {
console.log("Rotation: " + args.rotation);
});
var label = new labelModule.Label();
var observer = label.observe(gestures.GestureTypes.Rotation, function (args: gestures.RotationGestureEventData) {
console.log("Rotation: " + args.rotation);
});
To stop receiving information about gestures, simply call the disconnect
method of the observer object that you received when you called the observe
method.
var label = new labelModule.Label();
var observer = label.observe(gestures.GestureTypes.Tap, function (args) {
console.log("Tap");
});
observer.disconnect();
var label = new labelModule.Label();
var observer = label.observe(gestures.GestureTypes.Tap, function (args: gestures.GestureEventData) {
console.log("Tap");
});
observer.disconnect();