How to implement drag after long press? #1826
-
The docs say:
However, I can't figure out how to measure timing in the callbacks of the Here's what I have so far: const isPressed = useSharedValue(false);
const isDragging = useSharedValue(false);
const start = useSharedValue({
x: 0,
y: 0,
});
const offset = useSharedValue({
x: 0,
y: 0,
});
const gesture =
Gesture.Pan()
.manualActivation(true)
.onBegin((evt) => {
console.log('pan begin');
})
.onStart(() => {
console.log('pan start');
isPressed.value = true;
offset.value = {
x: 0,
y: 0,
};
})
.onTouchesDown((evt, state) => {
if (evt.numberOfTouches !== 1) {
state.fail();
}
isPressed.value = true;
start.value = {
x: evt.allTouches[0].x,
y: evt.allTouches[0].y,
};
// using setTimeout here causes a crash, and using runOnJS doesn't fix it
// runOnJS(setTimeout)(() => {
// isDragging.value = true;
// state.activate();
// }, 500);
})
.onTouchesMove((evt, state) => {
isPressed.value = true;
const offsetX = start.value.x - evt.allTouches[0].x;
const offsetY = start.value.y - evt.allTouches[0].y;
const dist = Math.sqrt(offsetX * offsetX + offsetY * offsetY);
if (dist > 10) {
state.fail();
}
})
.onUpdate((evt) => {
offset.value = {
x: evt.translationX,
y: evt.translationY,
};
})
.onFinalize(() => {
offset.value = {
x: 0,
y: 0,
};
isPressed.value = false;
isDragging.value = false;
console.log('pan finalize');
}); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 16 replies
-
well, you're supposed to use composition gestures . Passed a Gesture.Exclusive( longPressGesture, panGesture ) to the target View . |
Beta Was this translation helpful? Give feedback.
well, you're supposed to use composition gestures . Passed a Gesture.Exclusive( longPressGesture, panGesture ) to the target View .
Make a SharedValue to record the state whether long press activated . So , you can activate the pan gesture manually inside the onTouchesMove callback when the long-press state is true . With this little change , you can get what you want as your question described .