ts-combined-gestures.md 4.1 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
## GestureMode
E
ester.zhou 已提交
21 22 23 24 25 26

| 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 已提交
27 28 29 30


## Events

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


## Example

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

  build() {
51
    Column() {
Z
zengyawen 已提交
52
      Text('sequence gesture\n' + 'LongPress onAction:' + this.count + '\nPanGesture offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY)
E
ester.zhou 已提交
53
        .fontSize(15)
54 55 56 57 58 59 60
    }
    .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 已提交
61
    .gesture(
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 89 90
      // 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 已提交
91
      .onCancel(() => {
92
        console.info('sequence gesture canceled')
Z
zengyawen 已提交
93 94 95 96 97 98
      })
    )
  }
}
```

99 100 101 102 103 104 105 106 107
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)