1.2K Star 6.5K Fork 5.7K

OpenHarmony/docs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ts-combined-gestures.md 4.37 KB
一键复制 编辑 原始数据 按行查看 历史
潜秋宏 提交于 2023-12-20 14:13 . 文档规范问题修改

组合手势

手势识别组合,即多种手势组合为复合手势,支持连续识别、并行识别和互斥识别。

说明:

从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。

接口

GestureGroup(mode: GestureMode, ...gesture: GestureType[])

  • 参数
    参数名 参数类型 必填 默认值 参数描述
    mode GestureMode - 设置组合手势识别模式。
    gesture TapGesture
    LongPressGesture
    PanGesture
    PinchGesture
    RotationGesture
    - 设置1个或者多个基础手势类型时,这些手势会被识别为组合手势。若此参数不填则组合手势识别功能不生效。
    说明:
    当需要为一个组件同时添加单击和双击手势时,可在组合手势中添加两个TapGesture,需要双击手势在前,单击手势在后,否则不生效。

GestureMode枚举说明

名称 描述
Sequence 顺序识别,按照手势的注册顺序识别手势,直到所有手势识别成功。若有一个手势识别失败,后续手势识别均失败。
Parallel 并发识别,注册的手势同时识别,直到所有手势识别结束,手势识别互相不影响。
Exclusive 互斥识别,注册的手势同时识别,若有一个手势识别成功,则结束手势识别。

事件

名称 功能描述
onCancel(event: () => void) 顺序组合手势(GestureMode.Sequence)取消后触发回调。

示例

// xxx.ets
@Entry
@Component
struct GestureGroupExample {
  @State count: number = 0
  @State offsetX: number = 0
  @State offsetY: number = 0
  @State positionX: number = 0
  @State positionY: number = 0
  @State borderStyles: BorderStyle = BorderStyle.Solid

  build() {
    Column() {
      Text('sequence gesture\n' + 'LongPress onAction:' + this.count + '\nPanGesture offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY)
        .fontSize(15)
    }
    .translate({ x: this.offsetX, y: this.offsetY, z: 0 })
    .height(150)
    .width(200)
    .padding(20)
    .margin(20)
    .border({ width: 3, style: this.borderStyles })
    .gesture(
      // 以下组合手势为顺序识别,当长按手势事件未正常触发时则不会触发拖动手势事件
      GestureGroup(GestureMode.Sequence,
        LongPressGesture({ repeat: true })
          .onAction((event: GestureEvent) => {
            if (event.repeat) {
              this.count++
            }
            console.info('LongPress onAction')
          })
          .onActionEnd(() => {
            console.info('LongPress end')
          }),
        PanGesture()
          .onActionStart(() => {
            this.borderStyles = BorderStyle.Dashed
            console.info('pan start')
          })
          .onActionUpdate((event: GestureEvent) => {
            this.offsetX = this.positionX + event.offsetX
            this.offsetY = this.positionY + event.offsetY
            console.info('pan update')
          })
          .onActionEnd(() => {
            this.positionX = this.offsetX
            this.positionY = this.offsetY
            this.borderStyles = BorderStyle.Solid
            console.info('pan end')
          })
      )
        .onCancel(() => {
          console.info('sequence gesture canceled')
        })
    )
  }
}

示意图:

按顺序首先触发长按事件:

zh-cn_image_0000001174104384

按顺序首先触发长按事件,长按事件识别结束之后,其次触发拖动事件,向右下方拖动:

zh-cn_image1_0000001174104384

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
其他
1
https://gitee.com/openharmony/docs.git
git@gitee.com:openharmony/docs.git
openharmony
docs
docs
OpenHarmony-3.2-Release

搜索帮助