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