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.
Drag with a mouse or a finger
The live preview below, like the web preview, recognizes gestures by the same rules as iOS and Android, from a mouse, a pen or a finger. Scan its QR code with Lynx Explorer to try them on a phone.
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. Like event handlers, they can change signals and add or remove elements.
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. - Register only the callbacks you need. Lynx skips a fling's
onStartandonEndunless anonBeginis registered, so AngularLynx registers an empty one for 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. For a pinch, build it from touch events instead:- Set
"multiTouch": truein the build target's options inangular.json. Without it, Lynx reports one finger at a time. - Work out the zoom from the fingers in
touchstart,touchmoveandtouchend. Read each event'schangedTouches, byidentifier, withpageXandpageY(xandyin the web preview). - Taps don't fire while two fingers are down.
- The Vitrine example's viewer (
src/app/artwork/viewer.ts) zooms this way.
- Set
- A double tap can't make a single tap wait.
numberOfTaps(2)works, butGesture.Exclusive(doubleTap, singleTap)on one element doesn't: Lynx recognizes each tap on its own. See Double taps. - A pan waiting for its
activeOffsetXalready holds the touch. AngularLynx holds back its callbacks, but Lynx activates the pan atminDistance. So the pan fails the element's other gestures before the offset is reached, unless they're simultaneous with it. See Options AngularLynx enforces. minPointers,maxPointersand a fling'snumberOfPointersdo nothing. Lynx follows one finger at a time.- A fling has no
onStart, and fires reliably only on an element that also has a pan. See FlingGesture. - 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
