ts-basic-gestures-pangesture.md 5.1 KB
Newer Older
E
ester.zhou 已提交
1 2


Z
zengyawen 已提交
3
# PanGesture
Z
zengyawen 已提交
4

5 6 7 8 9 10
**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 已提交
11

E
ester.zhou 已提交
12
## APIs
Z
zengyawen 已提交
13

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

E
ester.zhou 已提交
16
**Parameters**
Z
zengyawen 已提交
17

E
ester.zhou 已提交
18 19
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
20
| fingers | number | No| Minimum number of fingers to trigger a pan gesture. The value ranges from 1 to 10.<br>Default value: **1**<br>Value range: 1 to 10<br>**NOTE**<br>If the value is less than 1 or is not set, the default value is used.|
E
ester.zhou 已提交
21
| direction | PanDirection | No| Pan direction. The enumerated value supports the AND (&amp;) and OR (\|) operations.<br>Default value: **PanDirection.All**|
E
ester.zhou 已提交
22
| distance | number | No| Minimum pan distance to trigger the gesture, in vp.<br>Default value: **5**<br>**NOTE**<br>If a pan gesture and [tab](ts-container-tabs.md) swipe occur at the same time, set **distance** to **1** so that the gesture can be more easily recognized.|
Z
zengyawen 已提交
23

E
ester.zhou 已提交
24
## PanDirection
Z
zengyawen 已提交
25

E
ester.zhou 已提交
26 27 28 29 30 31 32 33 34 35
| 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 已提交
36 37


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

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

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

E
ester.zhou 已提交
44 45 46 47 48 49
**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**|
E
ester.zhou 已提交
50
| 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](ts-container-tabs.md) swipe occur at the same time, set **distance** to **1** so that the gesture can be more easily recognized.|
E
ester.zhou 已提交
51 52 53 54 55 56 57 58

**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 已提交
59 60 61 62


## Events

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


## Example

73 74
```ts
// xxx.ets
Z
zengyawen 已提交
75 76 77 78 79
@Entry
@Component
struct PanGestureExample {
  @State offsetX: number = 0
  @State offsetY: number = 0
E
ester.zhou 已提交
80 81 82
  @State positionX: number = 0
  @State positionY: number = 0
  private panOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Left | PanDirection.Right })
Z
zengyawen 已提交
83 84

  build() {
E
ester.zhou 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97
    Column() {
      Column() {
        Text('PanGesture offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY)
      }
      .height(200)
      .width(300)
      .padding(20)
      .border({ width: 3 })
      .margin(50)
      .translate({ x: this.offsetX, y: this.offsetY, z: 0 })
      // Pan left or right to trigger the gesture event.
      .gesture(
      PanGesture(this.panOption)
E
ester.zhou 已提交
98
        .onActionStart((event: GestureEvent) => {
Z
zengyawen 已提交
99 100
          console.info('Pan start')
        })
E
ester.zhou 已提交
101
        .onActionUpdate((event: GestureEvent) => {
E
ester.zhou 已提交
102 103
          this.offsetX = this.positionX + event.offsetX
          this.offsetY = this.positionY + event.offsetY
Z
zengyawen 已提交
104 105
        })
        .onActionEnd(() => {
E
ester.zhou 已提交
106 107
          this.positionX = this.offsetX
          this.positionY = this.offsetY
Z
zengyawen 已提交
108 109
          console.info('Pan end')
        })
E
ester.zhou 已提交
110 111 112 113 114 115 116 117 118
      )

      Button ('Set PanGesture Trigger Condition')
        .onClick(() => {
          // Set the pan gesture to be triggered by two fingers moving in any direction.
          this.panOption.setDirection(PanDirection.All)
          this.panOption.setFingers(2)
        })
    }
Z
zengyawen 已提交
119 120 121 122
  }
}
```

E
ester.zhou 已提交
123 124 125 126 127 128
**Diagrams**

Pannig to the left:

![en-us_image_0000001174264374](figures/en-us_image_0000001174264374.png) 

E
ester.zhou 已提交
129
Click **Set PanGesture Trigger Condition** to two fingers moving toward the lower left corner.
E
ester.zhou 已提交
130 131

 ![en-us_image1_0000001174264374](figures/en-us_image1_0000001174264374.png)