尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

HarmonyOS 多设备短视频开发 : 08 — 多设备手势交互的差异化处理策略

HarmonyOS 多设备短视频开发 : 08 — 多设备手势交互的差异化处理策略 08 — 多设备手势交互的差异化处理策略一、引言多设备短视频应用中手势交互是用户体验的关键。从手机触摸到智慧屏遥控器不同设备需要不同的手势策略。本文分析项目中的手势识别、冲突处理和设备差异化实现。二、手势识别类型2.1 TapGesture点击// 点击头像跳转个人作品页 MSVTextIcon({ src: $r(app.media.ic_user_avatar), iconSize: 44 }) .gesture(TapGesture().onAction(() { if (this.windowInfo.widthBp WidthBreakpoint.WIDTH_SM) { this.showSideIndividual true; // 宽屏侧边分栏 } this.pathStack.pushPathByName(IndividualByRouter, null); })) // 点击评论图标 .gesture(TapGesture().onAction(() { if (this.windowInfo.widthBp WidthBreakpoint.WIDTH_SM) { this.showSideComment true; this.pathStack.pushPathByName(SplitComment, null); } else { this.showComment true; // 窄屏半模态弹窗 } }))2.2 PanGesture拖拽用于进度条拖动.priorityGesture( PanGesture({ direction: PanDirection.Horizontal, distance: 5 }) .onActionStart((event) { this.isSeeking true; }) .onActionUpdate((event) { this.updateSeekByPosition(event.fingerList[0].localX); }) .onActionEnd(() { this.isSeeking false; }) )使用priorityGesture确保拖拽手势优先于外层手势。2.3 键盘事件智慧屏智慧屏场景通过onKeyPreIme监听遥控器按键.onKeyPreIme((event: KeyEvent) { if (event.type KeyType.Down) { if (event.keyCode KeyCode.KEYCODE_DPAD_DOWN) { this.swiperController.showNext(); // 下键切换下一个视频 } if (event.keyCode KeyCode.KEYCODE_DPAD_UP) { this.swiperController.showPrevious(); // 上键切换上一个视频 } } return false; // 继续传递事件 })同时支持空格键播放/暂停.onKeyEvent((event: KeyEvent) { if (event.type KeyType.Up event.keyCode KeyCode.KEYCODE_SPACE) { this.controlPlayOrPause(); } })三、手势冲突处理3.1 侧边栏点击关闭当评论或个人作品侧边栏打开时点击主内容区域应关闭侧边栏.gesture(TapGesture()) .onGestureRecognizerJudgeBegin((event, current) { if (current.getType() GestureControl.GestureType.TAP_GESTURE || current.getType() GestureControl.GestureType.CLICK) { if (this.showSideComment || this.showSideIndividual) { this.pathStack.pop(); this.showSideComment false; this.showSideIndividual false; return GestureJudgeResult.REJECT; // 拦截手势不传递给子组件 } } return GestureJudgeResult.CONTINUE; // 继续传递 })3.2 半模态弹窗阻止当侧边栏打开时阻止主内容区域的点击穿透.hitTestBehavior(this.showSideComment || this.showSideIndividual ? HitTestMode.Block // 拦截所有触摸事件 : HitTestMode.Default) // 正常传递四、持握手势感知手机特有AdaptiveVideoForDefault组件集成了鸿蒙的持握手势识别能力根据左右手持握调整 UI 布局import { motion } from kit.MultimodalAwarenessKit; // 注册监听 aboutToAppear(): void { motion.on(holdingHandChanged, (status) { if (status RIGHT_HAND_HELD || status LEFT_HAND_HELD) { this.holdingHandStatus status; } }); } // 根据持握方向调整图标位置 if (this.holdingHandStatus motion.HoldingHandStatus.LEFT_HAND_HELD) { // 操作栏放在左侧 Column({ space: 16 }) { /* 点赞、评论、分享图标 */ } } else { // 操作栏放在右侧默认 Column({ space: 16 }) { /* 点赞、评论、分享图标 */ } }同时根据持握手设置导航栏位置.navBarPosition( this.holdingHandStatus RIGHT_HAND_HELD ? NavBarPosition.Start // 右手持握导航在左侧 : NavBarPosition.End // 左手持握导航在右侧 )五、设备差异化处理5.1 暂停/播放按钮if (this.currentState paused) { if (this.windowInfo.widthBp WidthBreakpoint.WIDTH_XS) { // 手表小圆形播放按钮 SymbolGlyph($r(sys.symbol.play_fill)) .fontSize(14) } else { // 其他设备大暂停图标 Image($r(app.media.ic_pause)) .height(new WidthBreakpointTypenumber(56, 56, 56, 56).getValue(this.windowInfo.widthBp)) } }六、最佳实践priorityGesture 优先拖拽进度条使用priorityGesture避免与 Swiper 滑动冲突GestureRecognizerJudgeBegin 拦截在侧边栏场景拦截手势实现点击关闭HitTestMode.Block 配合手势拦截与触摸命中测试配合使用确保可靠的交互阻断键盘事件补充智慧屏等非触摸设备通过键盘事件提供替代交互持握手势利用利用鸿蒙特有 API 优化单手持握体验
返回列表