鸿蒙ArkTS声明式开发:跨平台支持列表【组合手势】

组合手势

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

说明:

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

接口

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

参数:

参数名参数类型必填参数描述
mode[GestureMode]设置组合手势识别模式。 默认值:GestureMode.Sequence
gesture[TapGesture][LongPressGesture][PanGesture]

GestureMode枚举说明

名称描述
Sequence顺序识别,按照手势的注册顺序识别手势,直到所有手势识别成功。当有一个手势识别失败时,所有手势识别失败。 顺序识别手势组仅有最后一个手势可以响应onActionEnd。
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 && event.repeat) {
          this.count++
        }
        console.info('LongPress onAction')
      }),
    PanGesture()
      .onActionStart(() => {
        this.borderStyles = BorderStyle.Dashed
        console.info('pan start')
      })
      .onActionUpdate((event?: GestureEvent) => {
        if (event) {
          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

  • 16
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值