Question: Set Bounds on PanGestureHandler
See original GitHub issueI ran into an Issue building a Slider where clamping the Animated Value resulted in the Slider handle not moving immediately when you’re at the beginning or end of the Slider. But, the width of the Slider handle has to be accounted for in the interpolation so it won’t slide past the end.
Notice the Slider handle when it’s in the middle versus the ends:
The code:
this._onGestureEvent = Animated.event(
[
{
nativeEvent: {
translationX: this._translateX,
},
},
],
{
useNativeDriver: true,
},
);
translateX: this._translateX.interpolate({
inputRange: [0, SLIDER_WIDTH - SLIDER_HAHNDLE_WIDTH],
outputRange: [0, SLIDER_WIDTH - SLIDER_HAHNDLE_WIDTH],
extrapolate: 'clamp',
})
If I don’t use interpolate the Slider works perfectly except it will drag past the ends. What if there was a configuration prop to specify beginning and ending values where the gesture will cancel once it hits them?
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Question: Set Bounds on PanGestureHandler · Issue #110
I ran into an Issue building a Slider where clamping the Animated Value resulted in the Slider handle not moving immediately when you're...
Read more >How do I set the initial offset in a PanGestureHandler from ...
After reading the documentation several times I figured it out. It's simpler than expected :) <PanGestureHandler onGestureEvent={this.
Read more >PanGestureHandler | React Native Gesture Handler
A continuous gesture handler that can recognize a panning (dragging) gesture and track its movement. The handler activates when a finger is placed...
Read more >ScrollView from scratch with PanGestureHandler in ... - YouTube
In this tutorial we'll learn how to recreate the scroll behavior with just a PanGestureHandler component (from react-native-gesture-handler ...
Read more >Examples of React Native Gesture Handler - eduCBA
Guide to React Native Gesture Handler. Here we discuss the introduction, syntax, uses, and examples along with examples and advantages.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Just to clarify this issue: using Animated’s interpolate with
extrapolate: 'clamp'
set output bounds for my Slider. My issue was logic concerning setting the offset of the Slider Handle once you dragged past the beginning or end of the Slider bounds and let go sincetranslationX
value will keep decrementing or incrementing as you “over-drag”. Once you over-drag you need to account for this and offset the Animated Value to either the start or end value which will work perfectly and not result in my issue above where your dragging the Handle but it is delayed in moving.I tried using
absoluteX
instead but it resulted in the slider not always following my finger. e.g. It would speed up and go past my finger once you got closer to the beginning or end, which isn’t ideal.@kmagiera Chime in here if you see anything I’ve said isn’t accurate. I did use your code for a lot of this dragging functionality. 😉
Figured it out! If
absoluteX
goes past the start or end of the Slider, I manually set offset to either 0 for the beginning orMAX_OUTPUT_RANGE
for the end so_translateX
will stop decrementing or incrementing and causing the delay once you try to drag again. These values—32
andwidth - 32
—are hardcoded for my testing purposes but they equate to the left and right offset of the Slider since usually your slider will have left and right “margin”.