Created
April 10, 2023 08:38
-
-
Save zhiqingchen/9de38961934b3672624fc6320b5deb03 to your computer and use it in GitHub Desktop.
replace PanResponder with react-native-gesture-handler by chatGPT-4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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