拓冰建站拓冰建站
首页 / 资讯中心 / 正文

HarmonyOS APP开发---“随手画“白板涂鸦App,需要用到这个库

HarmonyOS APP开发—随手画白板涂鸦App需要用到这个库做一个白板涂鸦 App手指画线要跟手、双指缩放画布、长按选区——RN 自带 PanResponder 在 JS 线程处理手势画线总是慢半拍。react-native-ohos/react-native-gesture-handler在原生层识别手势零延迟跟手。 仓库地址https://gitcode.com/CPF-RN/rntpc_react-native-gesture-handler 安装npm install react-native-ohos/react-native-gesture-handler写在前面随手画是一个简易白板涂鸦 App核心交互全靠手势单指拖拽画笔在画布上画线双指捏合缩放画布方便看细节或看全局双指拖拽平移画布长按选中已有笔画可移动或删除这些手势如果用 RN 自带的PanResponder问题很明显手势事件走 JS Bridge 中转画线延迟明显双指操作更是灾难。而且画线手势和画布平移手势会冲突——想画线结果把画布拖走了。react-native-gesture-handler把手势识别下沉到原生层在原生线程完成状态机管理JS 只接收最终结果——零延迟跟手还能用Gesture.Exclusive/Gesture.Simultaneous精确控制手势冲突。这篇文章聊什么画线手势——单指 Pan 驱动 SVG 路径绘制缩放平移——双指 Pinch Pan 同时识别手势冲突——Gesture.Exclusive 让画线和拖拽互不干扰单指双指捏合拖拽用户触摸画布几根手指?Pan 手势: 画线手势类型Pinch: 缩放画布Pan: 平移画布Simultaneous: 缩放平移同时画线 添加到路径列表渲染 SVG Path变换画布坐标第一步安装与根组件包裹npminstallreact-native-ohos/react-native-gesture-handlerimport { GestureHandlerRootView } from react-native-ohos/react-native-gesture-handler export default function App() { return ( GestureHandlerRootView style{{ flex: 1 }} Whiteboard / /GestureHandlerRootView ) }第二步单指画线手势用Gesture.Pan()驱动画笔轨迹配合react-native-svg的 Path 渲染import { GestureDetector, Gesture } from react-native-ohos/react-native-gesture-handler import { View } from react-native import { Path, Svg } from react-native-svg import { useSharedValue } from react-native-reanimated function Whiteboard() { const paths useSharedValueArraystring([]) const currentPath useSharedValue() // 单指画线手势 const drawGesture Gesture.Pan() .minPointers(1) .maxPointers(1) // 只识别单指 .onBegin((e) { currentPath.value M ${e.x} ${e.y} }) .onUpdate((e) { // 追加路径点 currentPath.value L ${e.x} ${e.y} }) .onEnd(() { // 画完一条线存入路径列表 paths.value [...paths.value, currentPath.value] currentPath.value }) return ( GestureDetector gesture{drawGesture} View style{{ flex: 1, backgroundColor: #fff }} Svg style{{ flex: 1 }} {/* 已完成的笔画 */} {paths.value.map((d, i) ( Path key{i} d{d} stroke#333 strokeWidth{3} fillnone / ))} {/* 当前正在画的笔画 */} {currentPath.value ? ( Path d{currentPath.value} stroke#4ECDC4 strokeWidth{3} fillnone / ) : null} /Svg /View /GestureDetector ) }手势在原生层识别onUpdate实时回调坐标——画线跟手零延迟比 PanReceiver 快一个量级。第三步双指缩放 平移同时识别双指操作画布捏合缩放 拖拽平移两个手势要同时识别// 缩放手势 const pinchGesture Gesture.Pinch() .onUpdate((e) { scale.value e.scale }) // 双指平移手势 const panGesture Gesture.Pan() .minPointers(2) // 至少两指 .onUpdate((e) { translateX.value e.translationX translateY.value e.translationY }) // 关键Simultaneous 让缩放和平移同时生效 const canvasGesture Gesture.Simultaneous(pinchGesture, panGesture)Gesture.Simultaneous是核心——缩放和平移互不阻塞用户可以一边捏合缩放一边拖拽平移画布体验和原生画板一样流畅。第四步手势冲突——画线 vs 画布拖拽单指画线 vs 双指拖拽用Gesture.Exclusive确保互斥// 组合画线单指和画布操作双指互斥 const combinedGesture Gesture.Exclusive(drawGesture, canvasGesture) GestureDetector gesture{combinedGesture} View style{{ flex: 1, backgroundColor: #fff }} {/* 画布内容 */} /View /GestureDetectorExclusive让手势按优先级判断——单指触发画线双指触发画布操作不会出现想画线结果把画布拖走的冲突。为什么随手画选了 Gesture Handler需求RN PanResponderreact-native-ohos/react-native-gesture-handler手势延迟JS 线程慢半拍✅ 原生层零延迟多指手势❌ 难处理✅ Pan/Pinch 原生支持手势同时❌✅Simultaneous手势互斥❌ 手动判断✅Exclusive与 SVG 联动手动✅ 原生回调驱动总结随手画这个场景里react-native-ohos/react-native-gesture-handler解决了三件事零延迟画线——Pan 手势在原生层识别onUpdate实时驱动 SVG Path双指缩放平移——Simultaneous让 Pinch 和 Pan 同时生效手势不冲突——Exclusive让单指画线和双指操作互斥各管各的如果你也在做绘画、地图、卡片交互等需要复杂手势的 RN 鸿蒙应用react-native-gesture-handler是手势处理的基石。
分享:

看完干货,该让你的企业上线了

免费需求沟通 · 48 小时内出具建站方案 · 河南本地可上门