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


4
> **NOTE**<br>
Z
zengyawen 已提交
5 6 7 8
> Combined gestures are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.


## Required Permissions
Z
zengyawen 已提交
9 10

None
Z
zengyawen 已提交
11

Z
zengyawen 已提交
12 13 14 15 16 17

## APIs

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

- Parameters
18
  | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
19
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
20 21
  | mode | GestureMode | Yes | - | Recognition mode of combined gestures. |
  | 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 已提交
22 23

- GestureMode enums
24
  | Name | Description | 
Z
zengyawen 已提交
25
  | -------- | -------- |
E
esterzhou 已提交
26 27 28
  | 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 已提交
29 30 31 32


## Events

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


## Example

Z
zengyawen 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

```
@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')
      })
    )
  }
}
```

Z
zengyawen 已提交
84
![en-us_image_0000001212058490](figures/en-us_image_0000001212058490.gif)