Gestures
Gestures recognize drags, swipes, taps and long presses on an element, and decide which one wins when several could match. Reach for them when a plain (bindtap) isn't enough.
Try gestures on a phone
Gestures run only on iOS and Android. The web preview ignores them, and so does the live preview below. Scan its QR code with Lynx Explorer to try them.
When to use a gesture
A gesture adds rules that events don't have: how far a finger must move, how long it must hold, and which gesture gets priority.
Attach a gesture
src/app/like-button.ts
- Import
LynxGestureDetectornext toLYNX_ELEMENTS. It provides the[lynxGesture]directive. - Build each gesture with chained calls.
new TapGesture()creates it, an option such as.maxDistance(10)sets a rule, and a callback such as.onEnd()reacts. Each call returns the same gesture. - Create each gesture once, as a
readonlyfield. The directive registers its gestures again whenever its input changes. - Callbacks run on the main thread, inside the native gesture. Changing signals there is safe.
- Keep the number of elements fixed while a gesture runs. Restyle elements from a callback, but don't add or remove them: that can crash the app. Defer it with
setTimeout, as thesetTimeoutrule explains.
Follow the gesture's lifecycle
A gesture calls you back at each stage:
- On a tap or long press,
onEndmeans success. Lynx also callsonEndwhen these gestures fail, for example when a tap turns into a drag. AngularLynx drops that call for you. - On a pan or fling, register
onBeginwhenever you useonStartoronEnd. An empty callback is enough. Without it, Lynx never callsonStartoronEnd, and doesn't warn you. - Every callback also gets a state manager as its second argument. It can end or fail the gesture from code. See Gesture lifecycle.
Combine gestures
An element can carry several gestures. By default, only one can be active at a time. The Gesture helpers set how they share the element:
- Order matters in
Exclusive. List the gesture that should win first. With the drag before the tap, a finger that moves becomes a drag, never a tap. - The helpers change the gestures you pass. Use each gesture in only one helper call.
- For finer control, call
waitFor(),simultaneousWith()orcontinueWith()on a single gesture. See the gestures API.
Complete example: a sticker you can drag or tap
src/app/sticker.ts
- The pan moves the sticker by updating two signals that feed a
transform. Restyling the same element on every frame is safe. onEndsaves where the sticker stopped, so the next drag starts from there instead of snapping back.- The tap only fires when the pan fails, which happens when the finger lifts before moving 10 pixels.
Limitations
- Pinch and rotation never fire.
PinchGestureandRotationGestureexist, but no Lynx platform recognizes them yet. Drive zoom from a pan or from on-screen buttons. numberOfTaps(2)doesn't make a double tap. No Lynx platform counts taps, so it fires on the first one. Compare the times of successive taps inonEndyourself.- If no gesture in the app fires on a device, check that your
lynx.config.tsdoesn't setenableNewGesturetofalse. AngularLynx turns it on by default, and Lynx ignores every gesture without it. See If no gesture fires.
Related
- Gestures API:
[lynxGesture]and theGesturehelpers - Gesture types: every gesture's options and event fields
- Gesture lifecycle: states, callbacks and the state manager
- Main-thread scripts: move elements on every frame with no lag
- Gestures example
