User Interface Gestures
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 on
and
off
methods that let you subscribe or unsubscribe
to all events and gestures recognized by the UI element.
As the first parameter, you pass an on
or
off
method and the type of gesture you want to
track. 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.
-
on( type Number | name String | names Comma separated String, callback Function... **)
- type - Number | name - String | names - Comma separated String
- callback - Function(args GestureEventData)
-
off( type Number | name String | names Comma separated String, callback Function... **)
- type - Number | name - String | names - Comma separated String
- callback - Function(args GestureEventData)
Tap
Action: Briefly touch the screen.
onTap(args: GestureEventData) {
console.log("Tap!");
}
<Label text="Tap here" (tap)="onTap($event)"></Label>
Double Tap
Action: Two taps in a quick succession.
onDoubleTap(args: GestureEventData) {
console.log("DoubleTap!");
}
<Label text="Double tap here" (doubleTap)="onDoubleTap($event)"></Label>
Possible implementation:
- Scale up the object with a predefined percentage, centered around the double-tapped region. If a user keeps repeating the double tap gesture, continue to scale up until the maximum scale is reached.
- Scale up the smallest targetable view or returns it to its original scale in nested views.
- Select text.
Long Press
Action: Press your finger against the screen for a few moments.
onLongPress(args: GestureEventData) {
console.log("LongPress!");
}
<Label text="Long press here" (longPress)="onLongPress($event)"></Label>
Possible implementation: Select one or more items in a view and act upon the data using a contextual action bar. Enter data selection mode. Avoid using long press for displaying contextual menus.
Swipe
Action: Swiftly slide your finger across the screen. Swipes are quick and affect the screen even after the finger is lifted off the screen.
onSwipe(args: SwipeGestureEventData) {
console.log("Swipe Direction: " + args.direction);
}
<Label text="Swipe here" (swipe)="onSwipe($event)"></Label>
Possible implementation: Navigate between views in the same hierarchy.
Pan
Action: Press your finger down and immediately start moving it around. Pans are executed more slowly and allow for more precision and the screen stops responding as soon as the finger is lifted off it.
onPan(args: PanGestureEventData) {
console.log("Pan delta: [" + args.deltaX + ", " + args.deltaY + "] state: " + args.state);
}
<Label text="Pan here" (pan)="onPan($event)"></Label>
Pinch
Action: Touch the screen using two of your fingers, then move them towards each other or away from each other.
onPinch(args: PinchGestureEventData) {
console.log("Pinch scale: " + args.scale + " state: " + args.state);
}
<Label text="Pinch here" (pinch)="onPinch($event)"></Label>
Possible implementation: Zoom into content or out of content.
Rotation
Action: Touch the screen using two of your fingers, then rotate them simultaneously left or right.
onRotate(args: RotationGestureEventData) {
console.log("Rotate angle: " + args.rotation + " state: " + args.state);
}
<Label text="Rotate here" (rotation)="onRotate($event)"></Label>
Touch
Action: A finger action was performed.
This is a general purpose gesture that is triggered whenever a
pointer (usually a finger) has performed a touch action (up,
down, move or cancel).
TouchGestureEventData
provides information about
all the pointers currently on the screen and their position
inside the view that triggered the event.
onTouch(args: TouchGestureEventData) {
console.log(
"Touch point: [" + args.getX() + ", " + args.getY() +
"] activePointers: " + args.getActivePointers().length);
}
<Label text="Touch here" (touch)="onTouch($event)"></Label>
Gestures Manipulations
In some scenarios, you would want to disable the user interaction or to create more complex UI where some gestures are passing through the parents of the actual interactive zone. NativeScript provides some specific properties for handling similar cases as follows:
-
isUserInteractionEnabled
- Gets or sets a boolean value indicating whether the user can interact with the view. Does not affect the appearance of the view. The default value istrue
. -
isEnabled
- Gets or sets a boolean value indicating whether the view is enabled. Affects the appearance of the view. The default value istrue
. -
isPassThroughParentEnabled
- Gets or sets a value indicating whether touch events should pass through to a parent view of the layout container in case an interactive child view did not handle the event. Does not affect the appearance of the view. The default value isfalse
.
*Note: *: There is a conceptual difference in how
isEnabled
is acting on Android and iOS. On Android, theisEnabled
set tofalse
(e.g., on Button) won't allow any events to pass through even whenisPassThroughParentEnabled
is set totrue
for its parent. On the contrary on iOS, the same setup will pass through the event to the parent.
Playground application demonstrating the usage of the three properties can be found here.