Skip to content

Instantly share code, notes, and snippets.

@zhiqingchen
Created April 10, 2023 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhiqingchen/9de38961934b3672624fc6320b5deb03 to your computer and use it in GitHub Desktop.
Save zhiqingchen/9de38961934b3672624fc6320b5deb03 to your computer and use it in GitHub Desktop.
replace PanResponder with react-native-gesture-handler by chatGPT-4
// https://github.com/wuba/react-native-echarts/blob/v1.0.0/src/utils/events.ts
// replace PanResponder with react-native-gesture-handler by chatGPT-4
import React from 'react';
import { Gesture, GestureDetector, State } from 'react-native-gesture-handler';
import { getInstance } from 'zrender/lib/zrender';
function dispatchEvent(
zrenderId,
types,
event,
stage,
props = {
zrX: event.x,
zrY: event.y,
}
) {
if (zrenderId) {
var handler = getInstance(zrenderId).handler;
types.forEach(function (type) {
handler.dispatch(type, {
preventDefault: () => {},
stopImmediatePropagation: () => {},
stopPropagation: () => {},
...props,
});
stage && handler.processGesture(event, stage);
});
}
}
export const GestureHandler = ({ zrenderId, children }) => {
const panGesture = Gesture.Pan()
.onStart((e) => {
dispatchEvent(zrenderId, ['mousedown', 'mousemove'], e, 'start');
})
.onUpdate((e) => {
dispatchEvent(zrenderId, ['mousemove'], e, 'change');
})
.onEnd((e) => {
dispatchEvent(zrenderId, ['mouseup', 'click'], e, 'end');
});
const pinchGesture = Gesture.Pinch()
.onUpdate((e) => {
const { scale, focalX, focalY } = e;
dispatchEvent(
zrenderId,
['mousewheel'],
e,
undefined,
{
zrX: focalX,
zrY: focalY,
zrDelta: (scale - 1) / 120,
}
);
});
return (
<GestureDetector
gesture={Gesture.Simultaneous(panGesture, pinchGesture)}
>
{children}
</GestureDetector>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment