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