ts-combined-gestures.md 3.8 KB
Newer Older
Z
zengyawen 已提交
1
# Combined Gestures
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
Continuous recognition, parallel recognition, and exclusive recognition are supported for a group of gestures.
Z
zengyawen 已提交
4

5
> **NOTE**
E
ester.zhou 已提交
6
>
7
> The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9 10 11 12 13 14

## APIs

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

- Parameters
E
ester.zhou 已提交
15
  | Name| Type| Mandatory| Default Value| Description|
Z
zengyawen 已提交
16
  | -------- | -------- | -------- | -------- | -------- |
17
  | mode | [GestureMode](#gesturemode)                                                  | Yes| - | Recognition mode of combined gestures.|
E
ester.zhou 已提交
18
  | gesture | [TapGesture](ts-basic-gestures-tapgesture.md)<br>\| [LongPressGesture](ts-basic-gestures-longpressgesture.md)<br>\| [PanGesture](ts-basic-gestures-pangesture.md)<br>\| [PinchGesture](ts-basic-gestures-pinchgesture.md)<br>\| [RotationGesture](ts-basic-gestures-rotationgesture.md) | Yes| - | Variable-length parameter, indicating one or more basic gesture types. These gestures are recognized in combination.|
Z
zengyawen 已提交
19

20 21 22 23 24 25
## GestureMode
| Name| Description|
| -------- | -------- |
| Sequence | Sequential recognition: Gestures are recognized in the registration sequence until all gestures are recognized successfully. When one gesture fails to be recognized, all gestures fail to be recognized.|
| Parallel | Parallel recognition. Registered gestures are recognized concurrently until all gestures are recognized. The recognition result of each gesture does not affect each other.|
| Exclusive | Exclusive recognition. Registered gestures are identified concurrently. If one gesture is successfully recognized, gesture recognition ends.|
Z
zengyawen 已提交
26 27 28 29


## Events

E
ester.zhou 已提交
30
| Name| Description|
Z
zengyawen 已提交
31
| -------- | -------- |
E
ester.zhou 已提交
32
| onCancel(event: () =&gt; void) | Callback for the GestureMode.Sequence cancellation event.|
Z
zengyawen 已提交
33 34 35 36


## Example

E
ester.zhou 已提交
37 38
```ts
// xxx.ets
Z
zengyawen 已提交
39 40 41 42 43 44
@Entry
@Component
struct GestureGroupExample {
  @State count: number = 0
  @State offsetX: number = 0
  @State offsetY: number = 0
45 46
  @State positionX: number = 0
  @State positionY: number = 0
E
ester.zhou 已提交
47
  @State borderStyles: BorderStyle = BorderStyle.Solid
Z
zengyawen 已提交
48 49

  build() {
50
    Column() {
Z
zengyawen 已提交
51
      Text('sequence gesture\n' + 'LongPress onAction:' + this.count + '\nPanGesture offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY)
52 53 54 55 56 57 58
    }
    .translate({ x: this.offsetX, y: this.offsetY, z: 0 })
    .height(150)
    .width(200)
    .padding(20)
    .margin(20)
    .border({ width: 3, style: this.borderStyles })
Z
zengyawen 已提交
59
    .gesture(
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
      // The following combined gestures are recognized in sequential recognition mode. If the long press gesture event is not triggered correctly, the drag gesture event will not be triggered.
    GestureGroup(GestureMode.Sequence,
    LongPressGesture({ repeat: true })
      .onAction((event: GestureEvent) => {
        if (event.repeat) {
          this.count++
        }
        console.info('LongPress onAction')
      })
      .onActionEnd(() => {
        console.info('LongPress end')
      }),
    PanGesture()
      .onActionStart(() => {
        this.borderStyles = BorderStyle.Dashed
        console.info('pan start')
      })
      .onActionUpdate((event: GestureEvent) => {
        this.offsetX = this.positionX + event.offsetX
        this.offsetY = this.positionY + event.offsetY
        console.info('pan update')
      })
      .onActionEnd(() => {
        this.positionX = this.offsetX
        this.positionY = this.offsetY
        this.borderStyles = BorderStyle.Solid
        console.info('pan end')
      })
    )
Z
zengyawen 已提交
89
      .onCancel(() => {
90
        console.info('sequence gesture canceled')
Z
zengyawen 已提交
91 92 93 94 95 96
      })
    )
  }
}
```

97 98 99 100 101 102 103 104 105
Diagram:

In sequence recognition mode the long press gesture event is triggered first.

![en-us_image_0000001174104384](figures/en-us_image_0000001174104384.png)

After the long press gesture is recognized, the drag gesture event is triggered.

 ![en-us_image1_0000001174104384](figures/en-us_image1_0000001174104384.png)