Loading…
Making Your React Native Gestures Feel Natural
2023-10-18
- Source
- Shopify
- Published
- Added to Yomu
Summary
The post describes techniques for making draggable elements in a React Native Sheet component feel more natural during open-and-close gestures. A gesture handler updates yPosition as the user’s finger moves, then chooses an open or closed destination when the drag ends, but a position-only decision can ignore a fast flick and a fixed 600 ms animation ignores distance. The revised interaction adds event.velocityY, weighted by a multiplier, to the destination decision and uses withSpring so distance and release velocity affect the animation. It also divides overdrag distance when the Sheet is already open, creating resistance that communicates the boundary, and reports that flicks become easier while movement feels more realistic; the examples are ultimately recommended for testing on a real device.
Context
The initial Sheet interaction decides whether to open or close based only on the finger’s position, so a quick downward flick may not close it. Its fixed 600 ms animation also takes the same time regardless of distance, and unrestricted dragging beyond the open position provides no resistance or indication that the boundary has been reached.
Approach / What changed
The gesture handler incorporates event.velocityY, with a multiplier controlling how much velocity influences the open-or-close decision. The component uses withSpring instead of a fixed-duration animation so distance and drag velocity affect the motion. When the Sheet is already open, the dragged distance is divided so the element moves only a fraction of the finger’s movement, adding resistance.
Takeaways
- event.velocityY is added to the Sheet’s position, with a multiplier controlling how strongly a quick flick affects whether it opens or closes.
- withSpring accounts for distance and velocity, replacing the same 600 ms duration for every animation and producing motion described as more physically realistic.
- Dividing overdrag distance makes the Sheet resist movement beyond its open position, communicating that further upward dragging has no additional functionality.