ts-basic-gestures-pangesture.md 3.6 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 10

## Required Permissions
Z
zengyawen 已提交
11 12

None
Z
zengyawen 已提交
13

Z
zengyawen 已提交
14 15 16

## APIs

17
PanGesture(options?: { fingers?: number, direction?: PanDirection, distance?: number } | [PanGestureOptions](#pangestureoptions))
Z
zengyawen 已提交
18 19

- Parameters
20
  | Name| Type| Mandatory| Default Value| Description|
Z
zengyawen 已提交
21
  | -------- | -------- | -------- | -------- | -------- |
22 23 24
  | fingers | number | No| 1 | Minimum number of fingers to trigger a pan gesture. The value ranges from 1 to 10.|
  | direction | PanDirection | No| All | Pan direction. The enumerated value supports the AND (&) and OR (\|)|operations.|
  | distance | number | No| 5.0 | Minimum pan distance to trigger the gesture, in vp.<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 已提交
25 26

- PanDirection enums
27
  | Name| Description|
Z
zengyawen 已提交
28
  | -------- | -------- |
29 30 31 32 33 34 35 36
  | 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 已提交
37 38


E
ester.zhou 已提交
39
### PanGestureOptions
Z
zengyawen 已提交
40

41
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 已提交
42

E
ester.zhou 已提交
43
PanGestureOptions(options?: { fingers?: number, direction?: PanDirection, distance?: number })
Z
zengyawen 已提交
44 45

- Parameters
46 47 48
  
For details, see **PanGesture**.
  
Z
zengyawen 已提交
49
- APIs
50
  | Name| Description|
Z
zengyawen 已提交
51
  | -------- | -------- |
52 53 54
  | setDirection(value: PanDirection) | Sets the direction.|
  | setDistance(value: number) | Sets the distance.|
  | setFingers(value: number) | Sets the number of fingers.|
Z
zengyawen 已提交
55 56 57 58


## Events

59
| Name| Description|
Z
zengyawen 已提交
60
| -------- | -------- |
61 62 63 64
| onActionStart(callback: (event?: GestureEvent) =&gt; void) | Callback invoked when a pan gesture is recognized.|
| onActionUpdate(callback: (event?: GestureEvent) =&gt; void) | Callback invoked when the pan gesture status is updated.|
| onActionEnd(callback: (event?: GestureEvent) =&gt; void) | Callback invoked when the finger used for a pan gesture is lift.|
| onActionCancel(callback: () =&gt; void) | Callback invoked when a tap cancellation event is received after a pan gesture is recognized.|
Z
zengyawen 已提交
65

66 67
- GestureEvent attributes related to the pan gesture
  | Name| Type| Description|
Z
zengyawen 已提交
68
  | -------- | -------- | -------- |
69 70
  | offsetX | number | Offset of the gesture event, in vp.|
  | offsetY | number | Offset of the gesture event, in vp.|
Z
zengyawen 已提交
71 72 73 74


## Example

75 76
```ts
// xxx.ets
Z
zengyawen 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90
@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 已提交
91
        .onActionStart((event: GestureEvent) => {
Z
zengyawen 已提交
92 93
          console.info('Pan start')
        })
E
ester.zhou 已提交
94
        .onActionUpdate((event: GestureEvent) => {
Z
zengyawen 已提交
95 96 97 98 99 100 101 102 103 104 105
          this.offsetX = event.offsetX
          this.offsetY = event.offsetY
        })
        .onActionEnd(() => {
          console.info('Pan end')
        })
    )
  }
}
```

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