mport React from 'react';
import { View, StyleSheet } from 'react-native';
import { PanGestureHandler } from 'react-native-gesture-handler';
import Animated, { useAnimatedGestureHandler, useAnimatedStyle, withSpring } from 'react-native-reanimated';
const DragDropUI = () => {
const position = React.useRef(new Animated.ValueXY()).current;
const gestureHandler = useAnimatedGestureHandler({
onStart: (_, ctx) => {
ctx.startX = position.value.x;
ctx.startY = position.value.y;
},
onActive: (event, ctx) => {
position.value.x = ctx.startX + event.translationX;
position.value.y = ctx.startY + event.translationY;
},
onEnd: () => {
position.value.x = withSpring(0);
position.value.y = withSpring(0);
},
});
const animatedStyle = useAnimatedStyle(() => {
return {
transform: [
{ translateX: position.value.x },
{ translateY: position.value.y },
],
};
});
return (
<View style={styles.container}>
<PanGestureHandler onGestureEvent={gestureHandler}>
<Animated.View style={[styles.box, animatedStyle]} />
</PanGestureHandler>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
box: {
width: 100,
height: 100,
backgroundColor: 'blue',
},
});
export default DragDropUI;