pub struct SwipeableState<T: Clone + PartialEq + 'static> { /* private fields */ }Expand description
A generic swipeable state that tracks an animated offset and snaps to the nearest anchor point on release.
§Type Parameters
T- The anchor value type (e.g.DismissValue,usizefor pager pages).
§Usage
use repose_core::*;
let state = SwipeableState::new(
vec![(0.0, "start"), (-200.0, "end")],
SwipeableConfig::default(),
);
// Wire pointer events using on_pointer_down/move/up modifier callbacks:
// state.on_pointer_down(e.position.x)
// state.on_pointer_move(e.position.x)
// state.on_pointer_up()
// Read offset each frame:
let offset = state.offset();Implementations§
Source§impl<T: Clone + PartialEq + 'static> SwipeableState<T>
impl<T: Clone + PartialEq + 'static> SwipeableState<T>
Sourcepub fn new(anchors: Vec<(f32, T)>, config: SwipeableConfig) -> Self
pub fn new(anchors: Vec<(f32, T)>, config: SwipeableConfig) -> Self
Create a new swipeable state.
anchors - pairs of (offset_px, value) sorted by offset. The first anchor
is the initial position. At least one anchor is required.
Sourcepub fn offset(&self) -> f32
pub fn offset(&self) -> f32
Current animated offset in pixels. Call every frame that needs the value. Requests a re-draw while the spring is still running.
Sourcepub fn current_value(&self) -> T
pub fn current_value(&self) -> T
Resolve the nearest anchor value from the current offset.
Sourcepub fn snap_to(&self, off: f32)
pub fn snap_to(&self, off: f32)
Snap to an absolute offset instantly (used during active drag).
Sourcepub fn animate_to(&self, value: &T)
pub fn animate_to(&self, value: &T)
Animate to the offset corresponding to a target value.
Sourcepub fn is_animating(&self) -> bool
pub fn is_animating(&self) -> bool
Returns true if the spring is still running.
Sourcepub fn on_pointer_down(&self, axis_pos: f32)
pub fn on_pointer_down(&self, axis_pos: f32)
Call from on_pointer_down with the position along the drag axis.
Sourcepub fn on_pointer_move(&self, axis_pos: f32)
pub fn on_pointer_move(&self, axis_pos: f32)
Call from on_pointer_move with the position along the drag axis.
Sourcepub fn on_pointer_up(&self)
pub fn on_pointer_up(&self)
Call from on_pointer_up. Snaps to the appropriate anchor based on
position and velocity.