👆 一、HarmonyOS手势系统概述
HarmonyOS提供了强大的手势识别能力,支持从简单的点击到复杂的多指操作,为创建直观且响应式的用户界面奠定了基础。
1. 手势类型与核心API
| 手势类型 | 识别内容 | 典型应用场景 | 核心API |
|---|---|---|---|
| 点击手势 (Click) | 单次轻触屏幕 | 按钮操作、项目选择 | .onClick() |
| 双击手势 (DoubleClick) | 快速连续两次点击 | 放大/缩小、快速操作 | .onDoubleClick() |
| 长按手势 (LongPress) | 长时间按压 | 上下文菜单、拖拽准备 | .onLongPress() |
| 拖拽手势 (Pan) | 单指滑动 | 元素移动、滑动操作 | PanGesture() |
| 捏合手势 (Pinch) | 两指缩放 | 图片缩放、地图缩放 | PinchGesture() |
| 旋转手势 (Rotation) | 两指旋转 | 图片旋转、元素旋转 | RotationGesture() |
2. 开发准备与配置
在ArkTS文件中导入必要的手势模块:
import gesture from '@ohos.multimodalInput.gesture';
import { GestureEvent, GestureGroup, GestureMode } from '@ohos.ultimodalInput.gesture';
✋ 二、基础手势识别与处理
1. 简单手势处理
使用内置的便捷手势处理方法:
@Component
struct BasicGestureExample {
@State tapCount: number = 0;
@State isLongPressed: boolean = false;
@State scaleValue: number = 1.0;
build() {
Column() {
// 点击手势
Text(`点击次数: ${this.tapCount}`)
.fontSize(18)
.padding(20)
.backgroundColor(Color.Blue)
.onClick(() => {
this.tapCount++;
})
// 双击手势
Text('双击放大')
.fontSize(18)
.padding(20)
.backgroundColor(Color.Green)
.scale({ x: this.scaleValue, y: this.scaleValue })
.onDoubleClick(() => {
animateTo({ duration: 300 }, () => {
this.scaleValue = this.scaleValue === 1.0 ? 1.5 : 1.0;
});
})
// 长按手势
Text(this.isLongPressed ? '已长按' : '长按我')
.fontSize(18)
.padding(20)
.backgroundColor(this.isLongPressed ? Color.Red : Color.Gray)
.onLongPress(() => {
this.isLongPressed = true;
setTimeout(() => {
this.isLongPressed = false;
}, 1000);
})
}
.width('100%')
.height('100%')
.padding(20)
}
}
2. 高级手势配置
对于更复杂的手势需求,可以使用Gesture构造函数:
@Component
struct AdvancedGestureExample {
@State panX: number = 0;
@State panY: number = 0;
@State rotationAngle: number = 0;
@State pinchScale: number = 1.0;
build() {
Stack() {
// 可拖拽、旋转、缩放的组件
Image($r('app.media.draggable_image'))
.width(200)
.height(200)
.translate({ x: this.panX, y: this.panY })
.rotate({ angle: this.rotationAngle })
.scale({ x: this.pinchScale, y: this.pinchScale })
.gesture(
// 拖拽手势
PanGesture({ fingers: 1 })
.onActionStart((event: GestureEvent) => {
console.info('拖拽开始');
})
.onActionUpdate((event: GestureEvent) => {
this.panX += event.offsetX;
this.panY += event.offsetY;
})
.onActionEnd(() => {
console.info('拖拽结束');
// 添加回弹动画
animateTo({ duration: 300, curve: Curve.Spring }, () => {
this.panX = 0;
this.panY = 0;
});
})
)
.gesture(
// 旋转手势
RotationGesture({ fingers: 2 })
.onActionUpdate((event: GestureEvent) => {
this.rotationAngle += event.angle;
})
)
.gesture(
// 缩放手势
PinchGesture({ fingers: 2 })
.onActionUpdate((event: GestureEvent) => {
this.pinchScale *= event.scale;
// 限制缩放范围
this.pinchScale = Math.max(0.5, Math.min(3, this.pinchScale));
})
)
}
.width('100%')
.height('100%')
.onClick(() => {
// 点击重置
animateTo({ duration: 500 }, () => {
this.panX = 0;
this.panY = 0;
this.rotationAngle = 0;
this.pinchScale = 1.0;
});
})
}
}
🔄 三、复杂手势组合与冲突处理
1. 手势组合与优先级
使用GestureGroup管理多个手势的优先级和组合方式:
@Component
struct GesturePriorityExample {
@State offsetX: number = 0;
@State offsetY: number = 0;
@State scale: number = 1.0;
@State isScrolling: boolean = false;
build() {
Column() {
// 复杂手势组合示例
Column() {
Text('手势优先级示例')
.fontSize(20)
.margin({ bottom: 20 })
// 可交互区域
Stack() {

最低0.47元/天 解锁文章
9896

被折叠的 条评论
为什么被折叠?



