i have a Pan gesture handler inside a Pinch gesture handler to create the effects of multi directional scrolling zoomable view that is all working well, but i am having issues where when users zoom in the view is not zoomed in under their fingers when users zoom the view would zoom some place else and not keep the same position, here is my code:
This is the view:
<PinchGestureHandler
simultaneousHandlers={[panRef]}
ref={pinchRef}
onGestureEvent={pinchGestureHandler}>
<Animated.View style={animatedStyle}>
<PanGestureHandler
simultaneousHandlers={[pinchRef]}
ref={panRef}
onGestureEvent={panGestureHandler}></PanGestureHandler>
</Animated.View>
</PinchGestureHandler>
and here are my handlers:
const translateX = useSharedValue(0);
const translateY = useSharedValue(0);
const scale = useSharedValue(1);
const panRef = useRef();
const pinchRef = useRef();
const panGestureHandler = useAnimatedGestureHandler({
onStart: (_, ctx) => {
ctx.startX = translateX.value;
ctx.startY = translateY.value;
},
onActive: (event, ctx) => {
translateX.value = ctx.startX + event.translationX;
translateY.value = ctx.startY + event.translationY;
},
onEnd: (event, ctx) => {
translateX.value = withDecay({
velocity: event.velocityX,
deceleration: 0.99,
});
translateY.value = withDecay({
velocity: event.velocityY,
deceleration: 0.99,
});
},
});
const pinchGestureHandler = useAnimatedGestureHandler({
onStart: (_, ctx) => {
ctx.startScale = scale.value;
},
onActive: (event, ctx) => {
scale.value = ctx.startScale * event.scale;
},
onEnd: (event, ctx) => {
scale.value = withSpring(Math.min(Math.max(scale.value, 1), 3));
},
});
const animatedStyle = useAnimatedStyle(() => {
return {
transform: [
{translateX: translateX.value},
{translateY: translateY.value},
{scale: scale.value},
],
};
});
ATTEMPTS:
Tried adjusting the handlers to consider the focal points but the zooming is still way off from the point of the fingers
const pinchGestureHandler = useAnimatedGestureHandler({
onStart: (_, ctx) => {
ctx.startScale = scale.value;
ctx.startTranslateX = translateX.value;
ctx.startTranslateY = translateY.value;
},
onActive: (event, ctx) => {
scale.value = ctx.startScale * event.scale;
const focalX = event.focalX;
const focalY = event.focalY;
// Adjust translations
translateX.value = ctx.startTranslateX + (1 - event.scale) * (focalX - ctx.startTranslateX);
translateY.value = ctx.startTranslateY + (1 - event.scale) * (focalY - ctx.startTranslateY);
},
onEnd: (_) => {
scale.value = withSpring(Math.min(Math.max(scale.value, 1), 3));
},
});
from React-Native Pinch Gesture Handler Zoom Position
No comments:
Post a Comment