ts-combined-gestures.md 6.0 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4 5 6 7 8
# 组合手势<a name="ZH-CN_TOPIC_0000001193075118"></a>

>![](../../public_sys-resources/icon-note.gif) **说明:** 
>从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。

## 权限列表<a name="section781125411508"></a>


Z
zengyawen 已提交
9 10 11 12 13 14 15

## 接口<a name="section46581530142116"></a>

GestureGroup\(mode: GestureMode, ...gesture: GestureType\[\]\)

-   参数

Z
zengyawen 已提交
16
    <table><thead align="left"><tr><th class="cellrowborder" valign="top" width="16.11%" id="mcps1.1.6.1.1"><p>参数名</p>
Z
zengyawen 已提交
17
    </th>
Z
zengyawen 已提交
18
    <th class="cellrowborder" valign="top" width="20.330000000000002%" id="mcps1.1.6.1.2"><p>参数类型</p>
Z
zengyawen 已提交
19
    </th>
Z
zengyawen 已提交
20
    <th class="cellrowborder" valign="top" width="11.450000000000001%" id="mcps1.1.6.1.3"><p>必填</p>
Z
zengyawen 已提交
21
    </th>
Z
zengyawen 已提交
22
    <th class="cellrowborder" valign="top" width="9.35%" id="mcps1.1.6.1.4"><p>默认值</p>
Z
zengyawen 已提交
23
    </th>
Z
zengyawen 已提交
24
    <th class="cellrowborder" valign="top" width="42.76%" id="mcps1.1.6.1.5"><p>参数描述</p>
Z
zengyawen 已提交
25 26 27
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
28
    <tbody><tr><td class="cellrowborder" valign="top" width="16.11%" headers="mcps1.1.6.1.1 "><p>mode</p>
Z
zengyawen 已提交
29
    </td>
Z
zengyawen 已提交
30
    <td class="cellrowborder" valign="top" width="20.330000000000002%" headers="mcps1.1.6.1.2 "><p><a href="#li1176517215161">GestureMode</a></p>
Z
zengyawen 已提交
31
    </td>
Z
zengyawen 已提交
32
    <td class="cellrowborder" valign="top" width="11.450000000000001%" headers="mcps1.1.6.1.3 "><p>是</p>
Z
zengyawen 已提交
33
    </td>
Z
zengyawen 已提交
34
    <td class="cellrowborder" valign="top" width="9.35%" headers="mcps1.1.6.1.4 "><p>-</p>
Z
zengyawen 已提交
35
    </td>
Z
zengyawen 已提交
36
    <td class="cellrowborder" valign="top" width="42.76%" headers="mcps1.1.6.1.5 "><p>设置组合手势识别模式。</p>
Z
zengyawen 已提交
37 38
    </td>
    </tr>
Z
zengyawen 已提交
39
    <tr><td class="cellrowborder" valign="top" width="16.11%" headers="mcps1.1.6.1.1 "><p>gesture</p>
Z
zengyawen 已提交
40
    </td>
Z
zengyawen 已提交
41 42 43 44 45
    <td class="cellrowborder" valign="top" width="20.330000000000002%" headers="mcps1.1.6.1.2 "><p><a href="ts-basic-gestures-tapgesture.md">TapGesture</a></p>
    <p>| <a href="ts-basic-gestures-longpressgesture.md">LongPressGesture</a></p>
    <p>| <a href="ts-basic-gestures-pangesture.md">PanGesture</a></p>
    <p>| <a href="ts-basic-gestures-pinchgesture.md">PinchGesture</a></p>
    <p>| <a href="ts-basic-gestures-rotationgesture.md">RotationGesture</a></p>
Z
zengyawen 已提交
46
    </td>
Z
zengyawen 已提交
47
    <td class="cellrowborder" valign="top" width="11.450000000000001%" headers="mcps1.1.6.1.3 "><p>是</p>
Z
zengyawen 已提交
48
    </td>
Z
zengyawen 已提交
49
    <td class="cellrowborder" valign="top" width="9.35%" headers="mcps1.1.6.1.4 "><p>-</p>
Z
zengyawen 已提交
50
    </td>
Z
zengyawen 已提交
51
    <td class="cellrowborder" valign="top" width="42.76%" headers="mcps1.1.6.1.5 "><p>可变长参数,1个或者多个基础手势类型,这些手势会被组合识别。</p>
Z
zengyawen 已提交
52 53 54 55 56 57 58
    </td>
    </tr>
    </tbody>
    </table>

-   <a name="li1176517215161"></a>GestureMode枚举说明

Z
zengyawen 已提交
59
    <table><thead align="left"><tr><th class="cellrowborder" valign="top" width="25.2%" id="mcps1.1.3.1.1"><p>名称</p>
Z
zengyawen 已提交
60
    </th>
Z
zengyawen 已提交
61
    <th class="cellrowborder" valign="top" width="74.8%" id="mcps1.1.3.1.2"><p>描述</p>
Z
zengyawen 已提交
62 63 64
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
65
    <tbody><tr><td class="cellrowborder" valign="top" width="25.2%" headers="mcps1.1.3.1.1 "><p>Sequence</p>
Z
zengyawen 已提交
66
    </td>
Z
zengyawen 已提交
67
    <td class="cellrowborder" valign="top" width="74.8%" headers="mcps1.1.3.1.2 "><p>顺序识别,按照手势的注册顺序识别手势,直到所有手势识别成功。当有一个手势识别失败时,所有手势识别失败。</p>
Z
zengyawen 已提交
68 69
    </td>
    </tr>
Z
zengyawen 已提交
70
    <tr><td class="cellrowborder" valign="top" width="25.2%" headers="mcps1.1.3.1.1 "><p>Parallel</p>
Z
zengyawen 已提交
71
    </td>
Z
zengyawen 已提交
72
    <td class="cellrowborder" valign="top" width="74.8%" headers="mcps1.1.3.1.2 "><p>并发识别,注册的手势同时识别,直到所有手势识别结束,手势识别互相不影响。</p>
Z
zengyawen 已提交
73 74
    </td>
    </tr>
Z
zengyawen 已提交
75
    <tr><td class="cellrowborder" valign="top" width="25.2%" headers="mcps1.1.3.1.1 "><p>Exclusive</p>
Z
zengyawen 已提交
76
    </td>
Z
zengyawen 已提交
77
    <td class="cellrowborder" valign="top" width="74.8%" headers="mcps1.1.3.1.2 "><p>互斥识别,注册的手势同时识别,若有一个手势识别成功,则结束手势识别。</p>
Z
zengyawen 已提交
78 79 80 81 82 83 84 85
    </td>
    </tr>
    </tbody>
    </table>


## 事件<a name="section660220206578"></a>

Z
zengyawen 已提交
86
<table><thead align="left"><tr><th class="cellrowborder" colspan="2" valign="top" id="mcps1.1.4.1.1"><p>名称</p>
Z
zengyawen 已提交
87
</th>
Z
zengyawen 已提交
88
<th class="cellrowborder" valign="top" id="mcps1.1.4.1.2"><p>功能描述</p>
Z
zengyawen 已提交
89 90 91
</th>
</tr>
</thead>
Z
zengyawen 已提交
92
<tbody><tr><td class="cellrowborder" colspan="2" valign="top" headers="mcps1.1.4.1.1 "><p>onCancel(event: () =&gt; void)</p>
Z
zengyawen 已提交
93
</td>
Z
zengyawen 已提交
94
<td class="cellrowborder" valign="top" headers="mcps1.1.4.1.2 "><p>顺序组合手势(GestureMode.Sequence)取消后触发回调。</p>
Z
zengyawen 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
</td>
</tr>
</tbody>
</table>

## 示例<a name="section16900453182718"></a>

```
@Entry
@Component
struct GestureGroupExample {
  @State count: number = 0
  @State offsetX: number = 0
  @State offsetY: number = 0
  @State borderStyle: BorderStyle = BorderStyle.Solid

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
      Text('sequence gesture\n' + 'LongPress onAction:' + this.count + '\nPanGesture offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY)
    }.translate({ x: this.offsetX, y: this.offsetY, z: 5 })
    .height(100).width(200).padding(10).margin(80).border({ width: 1, style: this.borderStyle })
    .gesture(
      GestureGroup(GestureMode.Sequence,
        LongPressGesture({ repeat: true })
          .onAction((event: GestureEvent) => {
            if (event.repeat) {this.count++}
            console.log('LongPress onAction')
          })
          .onActionEnd(() => {
            console.log('LongPress end')
          }),
        PanGesture({})
          .onActionStart(() => {
            this.borderStyle = BorderStyle.Dashed
            console.log('pan start')
          })
          .onActionUpdate((event: GestureEvent) => {
            this.offsetX = event.offsetX
            this.offsetY = event.offsetY
            console.log('pan update')
          })
      )
      .onCancel(() => {
        console.log('sequence gesture canceled')
      })
    )
  }
}
```

![](figures/GestureGroup.gif)