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


E
esterzhou 已提交
4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
Z
zengyawen 已提交
5 6 7 8
> This gesture is 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

PanGesture(options?: { fingers?: number, direction?: PanDirection, distance?: number } | [PanGestureOption](#pangestureoption))

- Parameters
E
esterzhou 已提交
18
    | Name | Type | Mandatory | Default Value | Description | 
Z
zengyawen 已提交
19
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
20 21 22
  | fingers | number | No | 1 | Minimum number of fingers to trigger a long press gesture. The value ranges from 1 to 10. | 
  | direction | PanDirection | No | All | Slide direction. The enumerated value supports the AND (&) and OR (\|) operations. | 
  | distance | number | No | 5.0 | Minimum slide recognition distance, in vp. | 
Z
zengyawen 已提交
23 24 25 26

- PanDirection enums
    | Name | Description | 
  | -------- | -------- |
E
esterzhou 已提交
27 28 29 30 31 32 33 34
  | All | All directions. | 
  | Horizontal | Horizontal slide. | 
  | Vertical | Vertical slide. | 
  | Left | Slide to the left. | 
  | Right | Slide to the right. | 
  | Up | Slide up. | 
  | Down | Slide down. | 
  | None | Slide disabled. | 
Z
zengyawen 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48


### PanGestureOption

The attributes of the slide gesture recognizer can be dynamically modified using the **PanGestureOption** AP. This avoids modifying attributes through status variables, which will cause the UI to be refreshed.

PanGestureOption(options?: { fingers?: number, direction?: PanDirection, distance?: number })

- Parameters
  For details, see **PanGesture**.

- APIs
    | Name | Description | 
  | -------- | -------- |
E
esterzhou 已提交
49 50 51
  | setDirection(value: PanDirection) | Sets the direction. | 
  | setDistance(value: number) | Sets the distance. | 
  | setFingers(value: number) | Sets the number of fingers. | 
Z
zengyawen 已提交
52 53 54 55 56 57


## Events

  | Name | Description | 
| -------- | -------- |
E
esterzhou 已提交
58 59 60 61
| onActionStart(callback: (event?: PanGestureEvent) => void) | Callback for the pan gestures reorganization event. | 
| onActionUpdate(callback: (event?: PanGestureEvent) => void) | Callback invoked when a pan gesture is recognized. | 
| onActionEnd(callback: (event?: PanGestureEvent) => void) | Callback invoked when the finger used for a pan gesture is lift. | 
| onActionCancel(callback: () => void) | Callback invoked when a tap cancellation event is received after a pan gesture is recognized. | 
Z
zengyawen 已提交
62 63 64 65

- PanGestureEvent<sup>8+</sup> attributes
    | Name | Type | Description | 
  | -------- | -------- | -------- |
E
esterzhou 已提交
66 67
  | offsetX | number | Offset of the gesture event, in vp. | 
  | offsetY | number | Offset of the gesture event, in vp. | 
Z
zengyawen 已提交
68 69 70 71


## Example

Z
zengyawen 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

```
@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({})
Z
zengyawen 已提交
88
        .onActionStart((event: PanGestureEvent) => {
Z
zengyawen 已提交
89 90
          console.info('Pan start')
        })
Z
zengyawen 已提交
91
        .onActionUpdate((event: PanGestureEvent) => {
Z
zengyawen 已提交
92 93 94 95 96 97 98 99 100 101 102
          this.offsetX = event.offsetX
          this.offsetY = event.offsetY
        })
        .onActionEnd(() => {
          console.info('Pan end')
        })
    )
  }
}
```

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