Use PanGestureHandler as a Scrollview
Hello,
I'm trying to build an horizontal PanGestureHandler that behaves as an ScrollView. So basically I need to provide velocity once the gesture is released and have two snap points at the end of the View so the scroll is stopped when reaches the end.
Basically, something like this: https://i.imgur.com/EBi86wC.gif
I'm new to RN Animated world, so I'm struggling with providing velocity to the PanGesture. Is there some example or right approach about how to build it?
Thanks!
You can just use a scrollview for this. There's props to hide the scrollbar
Well, it's more complicated than that. I'm translating the View based on scroll Y position, but at the same time I need that the View is also scrollable on X axis. That's why I cannot use a normal Scrollview, because I need View to be scrollable even if it is outside of the layout.
https://github.com/wcandillon/react-native-redash
https://www.youtube.com/channel/UC806fwFWpiLQV5y-qifzHnA
@4yki Could you elaborate a little bit? I know William for a while, in fact, this case is inspired in a video he made. But the video is based on a translated Animated.View and I need that the View is both translated and scrollable.
Is there some property in redash that can help me to achieve it?
So you want to scroll the scroll view vertically, but also scrub a view horizontally? If you use RN’s built in gesture handlers, it won’t interfere with the scroll view, so then you can respond to changes in x at the same time as scrolling
@jacobp100 The interaction I'm trying to build is:
- The horizontal bar is translated on X axis based on Y position of the vertical scrollview. So that, when you reach a new category threshold on the horizontal bar is translated to show that category on position X '0'.
- The horizontal bar it also need to be scrollable on its own, but I cannot use a Scrollview for that. The reason is, the View is translated outside of the boundaries of the screen, so I cannot scroll back in that position. For that reason I need a PanGesture so I can translate again the bar whichever its position is.
Does it make sense?
const translateX = withDecay({ value: translationX, velocity: velocityX, state: gestureState, offset: offsetX });
This util from redash makes effect of scrollview. So when the gesture is released, the view will animate according to velocity value. The other things you want to do during active gesture could be implemented with interpolation functions. PanGesture handler gives everything you need. The result from the gif can be easily achieved. I don't fully understand what effect you try to implement. But the idea is simple - interpolate trasnlationX/Y, so the inputRange is you gesture boundaries, and outputRange - position of the animated view.
@4yki thanks, withDecay() seems to work fine, but if the animation is not completely finished and I drag the PanGesture while still translating, the position is reset to the original value. You know if there is there some way to avoid this behaviour and preserve the last X value while translating?
Regarding gesture boundaries, I not fully understand how can I implement interpolate in this case. Could you show me an example. This is what I have now:
` const index = new Value(0); const gestureX = useRef(new Value(0)); const state = useRef(new Value(-1)); const velocityX = useRef(new Value(200)); const offsetX = useRef(new Value(20));
console.log(tabs, 'que está llegando');
const onGestureEvent = useCallback( event( [ { nativeEvent: { translationX: gestureX.current, velocityX: velocityX.current, state: state.current } } ], { useNativeDriver: true } ), [] );
const transX = useRef( withDecay({ value: gestureX.current, velocity: velocityX.current, state: state.current }) ); `
This is the current behaviour https://media.giphy.com/media/J4myqsTmOegFgy1tqS/giphy.gif
This issue was not active for some time so I'm closing it. If you need further help please reply.