ts-combined-gestures.md 3.3 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

E
ester.zhou 已提交
5
>  **NOTE**
E
ester.zhou 已提交
6
>
E
ester.zhou 已提交
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 9 10


## Required Permissions
Z
zengyawen 已提交
11 12

None
Z
zengyawen 已提交
13

Z
zengyawen 已提交
14 15 16 17 18 19

## APIs

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

- Parameters
E
ester.zhou 已提交
20
  | Name| Type| Mandatory| Default Value| Description|
Z
zengyawen 已提交
21
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
22 23
  | 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 已提交
24 25

- GestureMode enums
E
ester.zhou 已提交
26
  | Name| Description|
Z
zengyawen 已提交
27
  | -------- | -------- |
E
ester.zhou 已提交
28 29 30
  | 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 已提交
31 32 33 34


## Events

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


## Example

E
ester.zhou 已提交
42 43
```ts
// xxx.ets
Z
zengyawen 已提交
44 45 46 47 48 49
@Entry
@Component
struct GestureGroupExample {
  @State count: number = 0
  @State offsetX: number = 0
  @State offsetY: number = 0
E
ester.zhou 已提交
50
  @State borderStyles: BorderStyle = BorderStyle.Solid
Z
zengyawen 已提交
51 52 53 54 55

  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 })
E
ester.zhou 已提交
56
    .height(100).width(200).padding(10).margin(80).border({ width: 1, style: this.borderStyles })
Z
zengyawen 已提交
57 58 59 60 61 62 63 64 65 66 67 68
    .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(() => {
E
ester.zhou 已提交
69
            this.borderStyles = BorderStyle.Dashed
Z
zengyawen 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
            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 已提交
86
![en-us_image_0000001212058490](figures/en-us_image_0000001212058490.gif)