HarmonyOS7 手势综合展示:六种手势类型一网打尽 + Grid 数据看板

发布时间:2026/7/23 22:16:48
HarmonyOS7 手势综合展示:六种手势类型一网打尽 + Grid 数据看板 文章目录前言效果展示方案设计多层手势共存策略详细实现状态变量日志函数Grid 统计看板statCard Builder三层手势绑定滑动方向判定日志面板踩坑记录写在最后前言前面十几篇文章分别讲了 ArkUI 的各种手势类型今天来一个全家桶——把点击、长按、双击、拖拽、缩放、旋转、滑动全部集成到一个界面里用 Grid 布局做数据看板实时显示每种手势的触发次数和状态。这篇文章既是手势系列的总结也是一个手势调试工具的设计参考。效果展示界面分三个区域统计看板6 个 Grid 格子分别显示点击/长按/双击/滑动/拖拽/缩放的计数或状态测试区域一个大色块支持所有手势操作日志面板记录最近 8 条手势操作的时间线方案设计多层手势共存策略一个组件上要同时支持 7 种手势怎么组织答案是分三层层级手势模式第一层双击、单击、长按Exclusive互斥第二层拖拽、缩放、旋转Parallel并行| 第三层 | 快速滑动 | 独立绑定 |三层手势用三个.gesture()调用分别绑定。ArkUI 允许多次调用.gesture()后面的不会覆盖前面的。详细实现状态变量EntryComponentstruct GestureShowcase{StatetapCount:number0StatelongPressCount:number0StateswipeDirection:string-StatepanOffset:string(0, 0)StatescaleValue:number1StateangleValue:number0StatedoubleTapCount:number0StategestureLog:string[][]// ...}每种手势对应一个状态变量gestureLog记录操作日志。日志函数logGesture(name:string){consttimenewDate().toLocaleTimeString()this.gestureLog.unshift([${time}]${name})if(this.gestureLog.length8){this.gestureLog.pop()}}每次手势触发时调用新日志插入数组头部超过 8 条就移除最旧的。unshiftpop组合实现了固定长度的 FIFO 队列。Grid 统计看板Grid(){GridItem(){this.statCard(点击,${this.tapCount},#4D96FF)}GridItem(){this.statCard(长按,${this.longPressCount},#FF6B6B)}GridItem(){this.statCard(双击,${this.doubleTapCount},#9B59B6)}GridItem(){this.statCard(滑动,this.swipeDirection,#6BCB77)}GridItem(){this.statCard(拖拽,this.panOffset,#FFA500)}GridItem(){this.statCard(缩放,${Math.floor(this.scaleValue*100)}%,#4ECDC4)}}.columnsTemplate(1fr 1fr 1fr).rowsTemplate(1fr 1fr).rowsGap(8).columnsGap(8).width(100%).height(160).margin({bottom:16})3 列 2 行的 Grid 布局每个格子用Builder函数statCard统一渲染。statCard BuilderBuilderstatCard(label:string,value:string,color:string){Column(){Text(value).fontSize(18).fontWeight(FontWeight.Bold).fontColor(color)Text(label).fontSize(10).fontColor(#999999).margin({top:2})}.width(100%).height(100%).backgroundColor(#F8F8F8).borderRadius(8).justifyContent(FlexAlign.Center).alignItems(HorizontalAlign.Center)}Builder是 ArkUI 的自定义构建函数可以在组件的build()方法里调用。这里传入标签、数值和颜色生成统一风格的统计卡片。三层手势绑定Column(){Text(手势测试区域).fontSize(16).fontColor(#FFFFFF)Text(点击/长按/双击/拖动/缩放/旋转).fontSize(11).fontColor(#FFFFFF).opacity(0.8)}.width(100%).height(140).backgroundColor(#667eea).borderRadius(16).justifyContent(FlexAlign.Center).alignItems(HorizontalAlign.Center).scale({x:this.scaleValue,y:this.scaleValue}).rotate({angle:this.angleValue})// 第一层互斥手势.gesture(GestureGroup(GestureMode.Exclusive,TapGesture({count:2}).onAction((){this.doubleTapCountthis.logGesture(双击)}),TapGesture({count:1}).onAction((){this.tapCountthis.logGesture(单击)}),LongPressGesture({duration:500}).onAction((){this.longPressCountthis.logGesture(长按)})))// 第二层并动手势.gesture(GestureGroup(GestureMode.Parallel,PanGesture({direction:PanDirection.All}).onActionUpdate((event:GestureEvent){this.panOffset(${Math.floor(event.offsetX)},${Math.floor(event.offsetY)})}).onActionEnd((){this.logGesture(拖拽)}),PinchGesture({fingers:2}).onActionUpdate((event:GestureEvent){this.scaleValueevent.scale}).onActionEnd((){this.logGesture(缩放)}),RotationGesture({fingers:2}).onActionUpdate((event:GestureEvent){this.angleValueevent.angle}).onActionEnd((){this.logGesture(旋转)})))// 第三层滑动手势.gesture(SwipeGesture({direction:SwipeDirection.All}).onAction((event:GestureEvent){constangleevent.angle??0if(angle45angle135)this.swipeDirection↑elseif(angle135angle225)this.swipeDirection←elseif(angle225angle315)this.swipeDirection↓elsethis.swipeDirection→this.logGesture(滑动)}))这段代码是手势系列知识的大综合。注意scale和rotate直接绑在组件上缩放和旋转操作能实时看到效果。滑动方向判定constangleevent.angle??0if(angle45angle135)this.swipeDirection↑elseif(angle135angle225)this.swipeDirection←elseif(angle225angle315)this.swipeDirection↓elsethis.swipeDirection→把 360° 分成四个 90° 的扇区每个扇区对应一个方向。比之前的左/右二分法更精细。日志面板Column(){Text(手势日志).fontSize(13).fontWeight(FontWeight.Bold).margin({bottom:8})ForEach(this.gestureLog,(log:string){Text(log).fontSize(11).fontColor(#999999).margin({bottom:2})})}.width(100%).margin({top:16}).padding(12).backgroundColor(#F8F8F8).borderRadius(8)时间戳 手势名称的列表最多 8 条。类似开发者工具的日志面板。踩坑记录问题原因解决双击不触发被单击抢走了双击写在单击前面Exclusive 模式按顺序判定拖拽和滑动冲突两者都是水平/垂直滑动用速度区分SwipeGesture 有速度阈值缩放后面板数字跳动缩放影响了布局只影响测试区域不影响统计面板多次调用.gesture()时手势之间的优先级取决于绑定顺序。先绑定的优先级高。如果发现某种手势死活触发不了检查一下是不是被另一个手势抢了。写在最后这个综合案例把手势系列的所有知识点串在了一起。核心设计思路是分层管理——互斥的放一层并行的放一层独立的放一层。配合 Grid 看板和日志面板不仅能验证手势是否正常工作还能直观看到每种手势的触发情况。做手势密集的界面时建议也做一个类似的调试面板开发效率会高很多。