Skip to content

Spring a snapping pan back when the finger rests before letting go - #223

Open
simonhamp wants to merge 1 commit into
mainfrom
fix/snap-velocity-after-hold
Open

simonhamp wants to merge 1 commit into
mainfrom
fix/snap-velocity-after-hold

Conversation

@simonhamp

Copy link
Copy Markdown
Member

On Android, a snapping one-axis pan (pan-x-snap / pan-y-snap) could fling to the next point even when the finger had stopped before it lifted. Found in the dev-null image viewer (NativePHP/dev-null-app#175), whose drag to close is a pan-y snapping to [-far, 0, far]: drag down, hold still for a moment, let go short of halfway, and it closed instead of springing back.

What was wrong

The snap picks its point from the velocity the drag lets go with. That velocity came from a Compose VelocityTracker fed only the move events the drag detectors pass on. A finger held still sends no moves, and the detectors end the drag without handing over the up event, so the tracker never learned the finger had stopped. It reported the speed of the last move however long the pause was.

The fix

A small ReleaseVelocity wraps the tracker in the snap path. Alongside the drag detector, the pan now watches for the finger lifting, in the Initial pass so the lift time is known before the detector ends the drag on the same event. If the last move was more than 40ms before the lift, the drag lets go with no velocity. That is the same rule Android's own VelocityTracker applies when it gets the up event. The watcher only runs when an axis snaps, so other pans are untouched.

Most of the renderer diff is the when block moving one indent level into a coroutineScope. It reads much smaller with whitespace hidden. The change stays out of the lines #220 edits, and the branch merges with it cleanly.

Testing

New GestureAreaSnapTest in the Robolectric module, with a paragraph in tests/android/README.md:

  • A quick drag to short of halfway, held still for 200ms, then let go, springs back to 0. This one fails on main (it lands on 600).
  • The same drag let go while moving still carries on to the far point.
  • A drag that pauses and then flicks again still carries on to the far point, so only the time between the last move and the lift counts.

The whole module passes (79 tests).

iOS

iOS has the same problem, at least in the simulator. I checked with a throwaway SwiftUI app that attaches a one-finger UIPanGestureRecognizer through UIGestureRecognizerRepresentable, as AxisPanRecognizer does, and an XCUITest that drags at 1500pt/s and then holds before lifting (iPhone 18 Pro simulator, iOS 27.0). velocity(in:) at .ended matched the last .changed every time: after holds of 0, 100ms, 300ms and 1s it reported 1394, 1360, 1403 and 1369 pt/s. UIKit doesn't decay it during the hold.

So AxisPanRecognizer hands the snap a stale velocity as well. On a real device a finger may jitter enough during a hold to send small moves that pull the velocity down, but I haven't checked that on hardware. This PR leaves iOS alone. The same check (time from the last move to the lift) would fit in AxisPanRecognizer's .ended.

🤖 Generated with Claude Code

On Android a snapping one-axis pan picked its point from a velocity
tracker that only ever saw move events. A finger held still and then
lifted kept the speed of its last move, so a drag let go short of
halfway still flung on to the next point.

The drag now also notes when the finger lifts, in the Initial pass so
it is known before the detector ends the drag. If the last move was
more than 40ms before the lift it lets go with no velocity, which is
how Android's own VelocityTracker treats the up event.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant