'React Native Animated Event not working in PanResponder
I am trying to create a swipe-able view in React Native (like queuing a song on Spotify) and I am getting unexpected behavior when calling Animated.Event. When I directly update my ref value, it works, however, when trying to use Animated Event, the view is not transformed as is expected.
I'm wondering if it is because swipeX is not an XY value and I am sending in the wrong arg mappings, but I'm not sure
const swipeX = useRef(new Animated.Value(0)).current;
const panResponder = useRef(
PanResponder.create({
onStartShouldSetPanResponder: (evt, gestureState) => true,
onPanResponderMove: (evt, gestureState) => {
if (gestureState.dx > 0) {
// this works
swipeX.setValue(
Math.min(gestureState.dx, 175)
)
}
// this doesn't work
Animated.event(
[
null,
{dx: swipeX}
],
{useNativeDriver: true}
)
},
onPanResponderTerminationRequest: (evt, gestureState) => true,
onPanResponderRelease: (evt, gestureState) => {
Animated.spring(swipeX, {
toValue: 0,
overshootClamping: true,
useNativeDriver: true
}).start()
},
})
).current;
element:
return (
<View>
<Animated.View
style={[
styles.container,
{
transform: [{translateX: swipeX}]
},
]}
{...panResponder.panHandlers}
>
<Text>Foo</Text>
</Animated.View>
</View>
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
