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