Skip to content

Gestures

Gestures have to feel instant, and an instant gesture cannot wait for a round trip to PHP on every frame. PAM Native therefore recognizes and tracks every gesture with the platform recognizers on the UI thread. PHP describes what should happen once, then receives a handful of semantic events: the gesture began, it ended, it settled on a snap point.

You have four tools, from simplest to most capable:

Tool Use it for
Pressable Taps, long presses, double taps and press feedback.
GestureDetector Tap, pan, pinch, rotation, swipe and long-press recognizers with semantic events.
Native transforms Zoomable and pannable photos without PHP in the frame loop.
Drag and Swipeable Drag-to-dismiss, swipe-to-reply and swipeable rows that settle with springs.

Pressable mirrors the React Native press model: pressIn, pressOut, press, longPress (after delayLongPress) and pressMove.

<Pressable
delayLongPress="260"
pressedOpacity="0.7"
@pressIn="armCapture"
@press="takePhoto"
@longPress="startRecording"
@pressOut="stopRecordingIfHeld"
accessibilityLabel="Shutter"
>
<View class="shutter" />
</Pressable>

A hold yields longPress and pressOut, never press as well; sliding off the control releases without either. Handlers typed with PressEvent receive the touch location (x, y relative to the pressable; pageX, pageY in the window):

use Pam\Native\PressEvent;
public function takePhoto(PressEvent $event): void
{
$this->focusPoint = [$event->x, $event->y];
}

Press feedback (pressedOpacity, pressedScale, pressedBackgroundColor, Android ripple) and CSS :pressed styles run natively; PHP is not involved.

Binding on:doubleTap makes the single press wait doubleTapDelay milliseconds (default 250) and cancels it when a second tap arrives, all on the UI thread:

<Pressable @press="togglePause" on:doubleTap="like" doubleTapDelay="250">
<Video :source="$clip->url" />
</Pressable>

A TapEffect can play a native animation (for example a heart burst) at the second tap before PHP is even notified; see Animations.

GestureDetector wraps one child and recognizes one gesture type:

use Pam\Native\GestureDirection;
use Pam\Native\GestureEvent;
use Pam\Native\GestureType;
use Pam\Native\UI\GestureDetector;
$card = GestureDetector::make(GestureType::Swipe, $cardView)
->direction(GestureDirection::Horizontal)
->minimumDistance(48)
->onEnd(function (GestureEvent $event): void {
$event->velocityX > 0 ? $this->accept() : $this->reject();
});
Option PHP Template Default
Type GestureType::Tap, Pan, Pinch, Rotation, Swipe, LongPress gestureType —
Direction direction(GestureDirection::Horizontal) (Any, Left, Right, Up, Down, Horizontal, Vertical) gestureDirection Any
Pointers pointers(1, 2) (1–10) gestureMinPointers, gestureMaxPointers 2 for pinch and rotation, else 1
Distance minimumDistance(12) (dp) gestureMinDistance pan 8, swipe 48, others 12
Hold minimumDuration(500) (ms) gestureMinDuration 500 for long press, else 0
Composition composition(GestureComposition::Simultaneous) gestureComposition Exclusive
Enabled gestureEnabled(false) gestureEnabled true

Events: onBegin, onUpdate, onEnd and onCancel (templates: on:gestureBegin, on:gestureUpdate, on:gestureEnd, on:gestureCancel). Each receives a GestureEvent with type, state, x, y, pageX, pageY, translationX, translationY, velocityX, velocityY (dp/s), scale, rotation, pointerCount and timestamp.

Pass item context alongside the event from templates; PAM decodes the payload according to the method’s GestureEvent type:

<GestureDetector gestureType="pan" on:gestureEnd="moveLayer($layer['id'], $event)">
<Layer :layer="$layer" />
</GestureDetector>
public function moveLayer(string $layerId, GestureEvent $event): void
{
$this->layers[$layerId]['x'] += $event->translationX;
$this->layers[$layerId]['y'] += $event->translationY;
}
  • Exclusive (default): this recognizer owns the touch once it begins.
  • Simultaneous: recognizes alongside scroll views and child recognizers.
  • Race: the first recognizer to begin owns the interaction.

Once movement recognizes a gesture, press and longPress on the same node are cancelled, so a swipe never also triggers a tap.

For photo viewers and canvases, let the native side apply the transform. With gestureNativeTransform, pan, pinch and rotation move the detector’s single child at the display refresh rate. Omit on:gestureUpdate and commit the result on end:

src/Components/PhotoViewer.pam.php
<template>
<GestureDetector
gestureType="pinch"
gestureNativeTransform="true"
gestureNativeMinScale="1"
gestureNativeMaxScale="4"
:gestureNativeResetKey="$photoRevision"
on:gestureEnd="commitZoom"
>
<Image :source="$url" resizeMode="contain" :accessibilityLabel="$caption" />
</GestureDetector>
</template>
public function commitZoom(GestureEvent $event): void
{
$this->zoom = $event->nativeScale ?? $this->zoom * $event->scale;
}

Increment gestureNativeResetKey when the displayed photo changes to return the transform to identity. nativeScale, nativeTranslationX and nativeTranslationY on the end event report the transform actually applied on screen; prefer them over multiplying relative deltas.

In PHP, the same options are ->nativeTransform(minimumScale: 1.0, maximumScale: 4.0, resetKey: $revision).

Add gestureNativeBounds="true" (and gestureNativeContentAspect for a contain image) to keep the scaled photo covering the viewport, spring it back to center at scale 1, and keep it inside the bounds when zoomed. gestureNativeFocalZoom="true" zooms around the fingers’ centroid. Pair a simultaneous pan with a simultaneous pinch on the same content to move and zoom at once:

<GestureDetector gestureType="pan" gestureComposition="simultaneous" gestureNativeTransform="true"
gestureNativeBounds="true" :gestureNativeContentAspect="$aspect" :gestureEnabled="$zoomed"
on:gestureEnd="commit">
<GestureDetector gestureType="pinch" gestureComposition="simultaneous" gestureNativeTransform="true"
gestureNativeMinScale="0.6" gestureNativeMaxScale="4" gestureNativeFocalZoom="true"
gestureNativeBounds="true" :gestureNativeContentAspect="$aspect" on:gestureEnd="commit">
<View :scaleX="$zoom" :scaleY="$zoom" :translationX="$x" :translationY="$y">
<Image :source="$uri" resizeMode="contain" accessibilityLabel="Photo" />
</View>
</GestureDetector>
</GestureDetector>

On Android, pinch and rotation claim the touch stream as soon as the second finger lands, so an ancestor ScrollView or pager cannot steal the zoom, while single-finger paging still works before it starts.

Drag turns a pan detector into a UI-thread drag. The target follows the finger on one axis inside optional bounds, other properties are interpolated from the translation every frame, and on release the target settles on a snap point with a spring or a timing curve. PHP receives only on:gestureBegin, on:gestureEnd (with snapIndex and thresholdReached) and on:gestureSettle.

src/Screens/StoryScreen.php
use Pam\Native\Animation\Drag;
use Pam\Native\Animation\Easing;
use Pam\Native\Animation\Spring;
use Pam\Native\GestureSettleEvent;
public Drag $dismiss;
public function __construct()
{
$this->dismiss = Drag::vertical()
->bounds(min: 0)
->snapPoints(0, '100%')
->threshold(120, velocity: 900)
->settle(new Spring(stiffness: 230, damping: 22, mass: 0.72))
->settleAt(1, Easing::EaseOut, 190)
->drive('', 'scale', [0, '50%'], [1, 0.955])
->drive('', 'borderRadius', [0, 120], [0, 18]);
}
public function settled(GestureSettleEvent $event): void
{
$event->snapIndex === 1 ? $this->close() : $this->resume();
}
<GestureDetector gestureType="pan" gestureDirection="vertical" gestureMinDistance="12"
:drag="$dismiss" on:gestureBegin="pause" on:gestureSettle="settled">
<StoryPage :story="$story" />
</GestureDetector>

Release rule: crossing threshold dp from the starting snap, or flinging faster than velocity dp/s toward the next snap, moves there; otherwise the drag springs back.

A single snap at 0 always springs back; the end event tells you whether the threshold was crossed, and haptic() ticks when it is:

use Pam\Native\Animation\Drag;
use Pam\Native\Animation\Spring;
use Pam\Native\GestureEvent;
$this->replySwipe = Drag::horizontal()
->bounds(min: 0, max: 72)
->target('bubble')
->snapPoints(0)
->threshold(46)
->haptic()
->settle(new Spring(260, 18))
->drive('replyIcon', 'opacity', [0, 46], [0, 1])
->drive('replyIcon', 'scale', [0, 46], [0.86, 1]);
public function swiped(GestureEvent $event): void
{
if ($event->thresholdReached) {
$this->reply($this->messageId);
}
}

target() and the first argument of drive() name elements marked with nativeRef="bubble" (PHP: ->nativeRef('bubble')); an empty string targets the detector’s child. Drivers accept opacity, translateX, translateY, scale, scaleX, scaleY, rotate and borderRadius; inputs are dp of drag or a percentage of the target’s extent.

Method Purpose
bounds(min:, max:), rubberBand($factor) Limits with optional rubber banding
snapPoints(...), threshold($dp, velocity:) Resting positions and the release rule
settle($curve, $ms), settleAt($index, $curve, $ms) A Spring, an Easing or a CSS timing string
drive(), driveRaw(), driveUnbounded() Interpolate other properties from the translation
driveText($ref, $input, $output, DragTextFormat) Update a label (number or clock) natively
atTouch($inset), snapOnRelease(false) Continuous controls such as a seek bar
group($name), haptic() One open member per group; a tick at the threshold

To snap programmatically, bind dragSnap="index@request" in templates (or GestureDetector::snapTo($index, $request)) and bump request to repeat the same snap.

Swipeable reveals action panels behind a row, settles open or closed with a spring and keeps one row per group open:

<Swipeable rightWidth="160" group="inbox" :closeRequest="$closeRows" on:gestureSettle="rowSettled">
<Row class="actions">
<Button label="Archive" @press="archive($conversation->id)" />
<Button label="Delete" @press="delete($conversation->id)" />
</Row>
<ConversationRow :conversation="$conversation" />
</Swipeable>

Children are [left panel], [right panel], then the content, in that order (panels only when their width is set). The content must paint an opaque background. In PHP: Swipeable::make($row)->rightActions($panel, 160)->group('inbox')->onOpen(fn (string $side) => ...). Give each row matching custom accessibility actions.

No gesture steals another’s drag. When the finger moves past the touch slop, a container gesture asks whether the content under the finger consumes that drag and yields to it:

  • the navigator’s back gesture yields to a scroller that can still scroll that way, a pan whose direction admits the drag, a slider, a drawing surface or a zoomed photo;
  • the edge back gesture only yields to exclusive owners (a drawing surface, a zoomed photo, a pan with gestureMinDistance="0");
  • a simultaneous dismiss pan yields to a list under the finger that can still scroll.

A view that pans on its own without telling the platform (a map, a signature pad, a plugin view) can block the back gesture while the finger is on it:

<View blocksNavigationGesture="true">
<SignaturePad />
</View>

Scroll views and lists report their lifecycle without per-frame traffic: on:scrollBeginDrag, on:scrollEndDrag (with release velocity) and on:momentumScrollEnd (with the settled page), each carrying a ScrollPhaseEvent. For carousels, on:pageChange fires once per page change, already during the drag:

<Scroll horizontal="true" pagingEnabled="true" on:pageChange="slideChanged">
<Slide p-for="$slide in $slides" :key="$slide->id" :slide="$slide" />
</Scroll>
use Pam\Native\PageChangeEvent;
public function slideChanged(PageChangeEvent $event): void
{
$this->activeSlide = $event->page;
}

Confirm a gesture with a short haptic:

use Pam\Native\HapticFeedback;
use Pam\Native\System\Haptics;
Haptics::trigger(HapticFeedback::Selection);

HapticFeedback has Selection, Light, Medium, Heavy, Success, Warning and Error. Use haptics sparingly; they cost battery and lose meaning when overused.