• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 组合手势
2<!--Kit: ArkUI-->
3<!--Subsystem: ArkUI-->
4<!--Owner: @jiangtao92-->
5<!--Designer: @piggyguy-->
6<!--Tester: @songyanhong-->
7<!--Adviser: @HelloCrease-->
8
9手势识别组合,即多种手势组合为复合手势,支持连续识别、并行识别和互斥识别。
10
11>  **说明:**
12>
13>  从API version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
14
15## 接口
16
17GestureGroup(mode: GestureMode, ...gesture: GestureType[])
18
19**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
20
21**系统能力:** SystemCapability.ArkUI.ArkUI.Full
22
23**参数:**
24
25| 参数名  | 类型                                                     | 必填 | 说明                                                     |
26| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
27| mode    | [GestureMode](#gesturemode枚举说明)                          | 是   | 设置组合手势识别模式。<br/>默认值:GestureMode.Sequence      |
28| gesture | [GestureType](./ts-gesture-common.md#gesturetype11-1)[] | 是   | 设置1个或者多个基础手势类型时,这些手势会被识别为组合手势。若此参数不填则组合手势识别功能不生效。<br/>**说明:**  <br/>当需要为一个组件同时添加单击和双击手势时,可在组合手势中添加两个TapGesture,需要双击手势在前,单击手势在后,否则不生效。 |
29
30## GestureMode枚举说明
31
32**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
33
34**系统能力:** SystemCapability.ArkUI.ArkUI.Full
35
36| 名称    | 值    | 说明                                       |
37| --------- | -------| ------------------------------------- |
38| Sequence | - | 顺序识别,按照手势的注册顺序识别手势,直到所有手势识别成功。若有一个手势识别失败,后续手势识别均失败。<br>顺序识别手势组仅有最后一个手势可以响应onActionEnd。 |
39| Parallel | - | 并发识别,注册的手势同时识别,直到所有手势识别结束,手势识别互相不影响。     |
40| Exclusive| - | 互斥识别,注册的手势同时识别,若有一个手势识别成功,则结束手势识别。       |
41
42
43## 事件
44
45**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
46
47**系统能力:** SystemCapability.ArkUI.ArkUI.Full
48
49### onCancel
50
51onCancel(event: () => void)
52
53手势识别成功,接收到触摸取消事件触发回调。
54
55**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
56
57**系统能力:** SystemCapability.ArkUI.ArkUI.Full
58
59**参数:**
60
61| 参数名 | 类型                                       | 必填 | 说明                         |
62| ------ | ------------------------------------------ | ---- | ---------------------------- |
63| event  |  () => void | 是   | 手势事件回调函数。 |
64
65## 示例
66
67该示例通过配置GestureGroup实现了长按和拖动的组合手势顺序识别。
68
69```ts
70// xxx.ets
71@Entry
72@Component
73struct GestureGroupExample {
74  @State count: number = 0;
75  @State offsetX: number = 0;
76  @State offsetY: number = 0;
77  @State positionX: number = 0;
78  @State positionY: number = 0;
79  @State borderStyles: BorderStyle = BorderStyle.Solid;
80
81  build() {
82    Column() {
83      Text('sequence gesture\n' + 'LongPress onAction:' + this.count + '\nPanGesture offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY)
84        .fontSize(15)
85    }
86    .translate({ x: this.offsetX, y: this.offsetY, z: 0 })
87    .height(150)
88    .width(200)
89    .padding(20)
90    .margin(20)
91    .border({ width: 3, style: this.borderStyles })
92    .gesture(
93      // 以下组合手势为顺序识别,当长按手势事件未正常触发时则不会触发拖动手势事件
94      GestureGroup(GestureMode.Sequence,
95        LongPressGesture({ repeat: true })
96          .onAction((event?: GestureEvent) => {
97            if (event && event.repeat) {
98              this.count++
99            }
100            console.info('LongPress onAction')
101          }),
102        PanGesture()
103          .onActionStart(() => {
104            this.borderStyles = BorderStyle.Dashed
105            console.info('pan start')
106          })
107          .onActionUpdate((event?: GestureEvent) => {
108            if (event) {
109              this.offsetX = this.positionX + event.offsetX
110              this.offsetY = this.positionY + event.offsetY
111            }
112            console.info('pan update')
113          })
114          .onActionEnd(() => {
115            this.positionX = this.offsetX
116            this.positionY = this.offsetY
117            this.borderStyles = BorderStyle.Solid
118            console.info('pan end')
119          })
120      )
121        .onCancel(() => {
122          console.info('sequence gesture canceled')
123        })
124    )
125  }
126}
127```
128
129示意图:
130
131按顺序首先触发长按事件:
132
133![zh-cn_image_0000001174104384](figures/zh-cn_image_0000001174104384.png)
134
135按顺序首先触发长按事件,长按事件识别结束之后,其次触发拖动事件,向右下方拖动:
136
137 ![zh-cn_image1_0000001174104384](figures/zh-cn_image1_0000001174104384.png)