ts-basic-gestures-pangesture.md 4.1 KB
Newer Older
Z
zengyawen 已提交
1
# PanGesture
Z
zengyawen 已提交
2

3 4 5 6 7 8
**PanGesture** is used to trigger a pan gesture, which requires a minimum 5 vp movement distance of a finger on the screen.

>  **NOTE**
>
>  This gesture is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.

Z
zengyawen 已提交
9

E
ester.zhou 已提交
10
## APIs
Z
zengyawen 已提交
11

E
ester.zhou 已提交
12
PanGesture(value?: { fingers?: number; direction?: PanDirection; distance?: number } | [PanGestureOptions](#pangestureoptions))
Z
zengyawen 已提交
13

E
ester.zhou 已提交
14
**Parameters**
Z
zengyawen 已提交
15

E
ester.zhou 已提交
16 17 18 19 20
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| fingers | number | No| Minimum number of fingers to trigger a pan gesture. The value ranges from 1 to 10.<br>Default value: **1**|
| direction | PanDirection | No| Pan direction. The enumerated value supports the AND (&amp;) and OR (\|) operations.<br>Default value: **PanDirection.All**|
| distance | number | No| Minimum pan distance to trigger the gesture, in vp.<br>Default value: **5.0**<br>**NOTE**<br>If a pan gesture and tab swipe occur at the same time, set **distance** to **1** so that the gesture can be more easily recognized.|
Z
zengyawen 已提交
21

E
ester.zhou 已提交
22
## PanDirection enums
Z
zengyawen 已提交
23

E
ester.zhou 已提交
24 25 26 27 28 29 30 31 32 33
| Name| Description|
| -------- | -------- |
| All | All directions.|
| Horizontal | Horizontal panning.|
| Vertical | Vertical panning.|
| Left | Panning to the left.|
| Right | Panning to the right.|
| Up | Panning up.|
| Down | Panning down.|
| None | Panning disabled.|
Z
zengyawen 已提交
34 35


E
ester.zhou 已提交
36
## PanGestureOptions
Z
zengyawen 已提交
37

38
The attributes of the pan gesture recognizer can be dynamically modified using the **PanGestureOptions** API. This avoids modifying attributes through state variables, which will cause the UI to be refreshed.
Z
zengyawen 已提交
39

E
ester.zhou 已提交
40
PanGestureOptions(value?: { fingers?: number; direction?: PanDirection; distance?: number })
Z
zengyawen 已提交
41

E
ester.zhou 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
**Parameters**

| Name | Type    | Mandatory| Description                                                    |
| --------- | ------------ | ---- | ------------------------------------------------------------ |
| fingers   | number       | No  | Minimum number of fingers to trigger a pan gesture. The value ranges from 1 to 10.<br>Default value: **1**|
| direction | PanDirection | No  | Pan direction. The enumerated value supports the AND (&amp;) and OR (\|) operations.<br>Default value: **All**|
| distance  | number       | No  | Minimum pan distance to trigger the gesture, in vp.<br>Default value: **5.0**<br>**NOTE**<br>If a pan gesture and tab swipe occur at the same time, set **distance** to **1** so that the gesture can be more easily recognized.|

**APIs**

| Name| Description|
| -------- | -------- |
| setDirection(value: PanDirection) | Sets the direction.|
| setDistance(value: number) | Sets the distance.|
| setFingers(value: number) | Sets the number of fingers.|
Z
zengyawen 已提交
57 58 59 60


## Events

61
| Name| Description|
Z
zengyawen 已提交
62
| -------- | -------- |
E
ester.zhou 已提交
63 64 65 66
| onActionStart(event: (event?: [GestureEvent](ts-gesture-settings.md)) =&gt; void) | Callback invoked when a pan gesture is recognized.|
| onActionUpdate(event: (event?: [GestureEvent](ts-gesture-settings.md)) =&gt; void) | Callback invoked when the pan gesture status is updated.|
| onActionEnd(event: (event?: [GestureEvent](ts-gesture-settings.md)) =&gt; void) | Callback invoked when the finger used for a pan gesture is lift.|
| onActionCancel(event: () =&gt; void) | Callback invoked when a tap cancellation event is received after a pan gesture is recognized.|
Z
zengyawen 已提交
67 68 69 70


## Example

71 72
```ts
// xxx.ets
Z
zengyawen 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86
@Entry
@Component
struct PanGestureExample {
  @State offsetX: number = 0
  @State offsetY: number = 0

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
      Text('PanGesture offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY)
    }
    .height(100).width(200).padding(20).border({ width: 1 }).margin(80)
    .translate({ x: this.offsetX, y: this.offsetY, z: 5 })
    .gesture(
      PanGesture({})
E
ester.zhou 已提交
87
        .onActionStart((event: GestureEvent) => {
Z
zengyawen 已提交
88 89
          console.info('Pan start')
        })
E
ester.zhou 已提交
90
        .onActionUpdate((event: GestureEvent) => {
Z
zengyawen 已提交
91 92 93 94 95 96 97 98 99 100 101
          this.offsetX = event.offsetX
          this.offsetY = event.offsetY
        })
        .onActionEnd(() => {
          console.info('Pan end')
        })
    )
  }
}
```

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