1e41f4b71Sopenharmony_ci
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci# PanGesture
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci拖动手势事件,当滑动的最小距离达到设定的最小值时触发拖动手势事件。
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci>  **说明:**
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci>  从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci## 接口
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ciPanGesture(value?: { fingers?: number, direction?: PanDirection, distance?: number } | [PanGestureOptions](#pangestureoptions))
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci**参数:**
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci| 参数名称 | 参数类型 | 必填 | 参数描述 |
21e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
22e41f4b71Sopenharmony_ci| fingers | number | 否 | 用于指定触发拖动的最少手指数,最小为1指,&nbsp;最大取值为10指。<br/>默认值:1<br/>取值范围:[1,10]<br/>**说明:** <br/>当设置的值小于1或不设置时,会被转化为默认值。 |
23e41f4b71Sopenharmony_ci| direction | [PanDirection](#pandirection枚举说明) | 否 | 用于指定触发拖动的手势方向,此枚举值支持逻辑与(&amp;)和逻辑或(\|)运算。<br/>默认值:PanDirection.All |
24e41f4b71Sopenharmony_ci| distance | number | 否 | 用于指定触发拖动手势事件的最小拖动距离,单位为vp。<br/>默认值:5<br/>**说明:**<br/>[Tabs组件](ts-container-tabs.md)滑动与该拖动手势事件同时存在时,可将distance值设为1,使拖动更灵敏,避免造成事件错乱。<br/>当设定的值小于0时,按默认值5处理。 |
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci## PanDirection枚举说明
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci与SwipeDirection不同,PanDirection没有角度限制。
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci| 名称 | 描述 |
33e41f4b71Sopenharmony_ci| -------- | -------- |
34e41f4b71Sopenharmony_ci| All | 所有方向。 |
35e41f4b71Sopenharmony_ci| Horizontal | 水平方向。 |
36e41f4b71Sopenharmony_ci| Vertical | 竖直方向。 |
37e41f4b71Sopenharmony_ci| Left | 向左拖动。 |
38e41f4b71Sopenharmony_ci| Right | 向右拖动。 |
39e41f4b71Sopenharmony_ci| Up | 向上拖动。 |
40e41f4b71Sopenharmony_ci| Down | 向下拖动。 |
41e41f4b71Sopenharmony_ci| None | 任何方向都不可触发拖动手势事件。 |
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci## PanGestureOptions
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci通过PanGestureOptions对象接口可以动态修改平移手势识别器的属性,从而避免通过状态变量修改属性(状态变量修改会导致UI刷新)。
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ciPanGestureOptions(value?: { fingers?: number, direction?: PanDirection, distance?: number })
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci**参数:**
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci| 参数名称  | 参数类型                              | 必填 | 参数描述                                                     |
55e41f4b71Sopenharmony_ci| --------- | ------------------------------------- | ---- | ------------------------------------------------------------ |
56e41f4b71Sopenharmony_ci| fingers   | number                                | 否   | 用于指定触发滑动的最少手指数,最小为1指,&nbsp;最大取值为10指。<br/>默认值:1 |
57e41f4b71Sopenharmony_ci| direction | [PanDirection](#pandirection枚举说明) | 否   | 用于指定设置滑动方向,此枚举值支持逻辑与(&amp;)和逻辑或(\|)运算。<br/>默认值:PanDirection.All |
58e41f4b71Sopenharmony_ci| distance  | number                                | 否   | 用于指定触发拖动手势事件的最小拖动距离,单位为vp。<br/>默认值:5<br/>**说明:**<br/>[Tabs组件](ts-container-tabs.md)滑动与该拖动手势事件同时存在时,可将distance值设为1,使拖动更灵敏,避免造成事件错乱。<br/>当设定的值小于0时,按默认值5处理。<br/>建议设置合理的拖动距离,拖动距离设置过大时会导致滑动不跟手(响应时延慢)的问题。 |
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci**接口**
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci| 名称 | 功能描述 |
63e41f4b71Sopenharmony_ci| -------- | -------- |
64e41f4b71Sopenharmony_ci| setDirection(value:&nbsp;[PanDirection](#pandirection枚举说明)) | 设置direction属性。 <br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。|
65e41f4b71Sopenharmony_ci| setDistance(value:&nbsp;number) | 设置distance属性。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
66e41f4b71Sopenharmony_ci| setFingers(value:&nbsp;number) | 设置fingers属性。 <br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。|
67e41f4b71Sopenharmony_ci| getDirection()<sup>12+</sup>: [PanDirection](#pandirection枚举说明) | 获取direction属性。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci## 事件
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci| 名称 | 功能描述 |
73e41f4b71Sopenharmony_ci| -------- | -------- |
74e41f4b71Sopenharmony_ci| onActionStart(event:&nbsp;(event:&nbsp;[GestureEvent](ts-gesture-settings.md#gestureevent对象说明))&nbsp;=&gt;&nbsp;void) | Pan手势识别成功回调。 <br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。|
75e41f4b71Sopenharmony_ci| onActionUpdate(event:&nbsp;(event:&nbsp;[GestureEvent](ts-gesture-settings.md#gestureevent对象说明))&nbsp;=&gt;&nbsp;void) | Pan手势移动过程中回调。<br/>fingerList为多根手指时,该回调监听每次只会更新一根手指的位置信息。 <br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。|
76e41f4b71Sopenharmony_ci| onActionEnd(event:&nbsp;(event:&nbsp;[GestureEvent](ts-gesture-settings.md#gestureevent对象说明))&nbsp;=&gt;&nbsp;void) | Pan手势识别成功,手指抬起后触发回调。 <br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。|
77e41f4b71Sopenharmony_ci| onActionCancel(event:&nbsp;()&nbsp;=&gt;&nbsp;void) | Pan手势识别成功,接收到触摸取消事件触发回调。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。|
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci## 属性
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci| 名称 | 类型    |描述                                        |
84e41f4b71Sopenharmony_ci| ----  | ------  | ---------------------------------------- |
85e41f4b71Sopenharmony_ci| tag<sup>11+</sup>   | string  | 设置Pan手势标志,用于自定义手势判定时区分绑定的手势。|
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ci## 示例
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci```ts
90e41f4b71Sopenharmony_ci// xxx.ets
91e41f4b71Sopenharmony_ci@Entry
92e41f4b71Sopenharmony_ci@Component
93e41f4b71Sopenharmony_cistruct PanGestureExample {
94e41f4b71Sopenharmony_ci  @State offsetX: number = 0
95e41f4b71Sopenharmony_ci  @State offsetY: number = 0
96e41f4b71Sopenharmony_ci  @State positionX: number = 0
97e41f4b71Sopenharmony_ci  @State positionY: number = 0
98e41f4b71Sopenharmony_ci  private panOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Left | PanDirection.Right })
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci  build() {
101e41f4b71Sopenharmony_ci    Column() {
102e41f4b71Sopenharmony_ci      Column() {
103e41f4b71Sopenharmony_ci        Text('PanGesture offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY)
104e41f4b71Sopenharmony_ci      }
105e41f4b71Sopenharmony_ci      .height(200)
106e41f4b71Sopenharmony_ci      .width(300)
107e41f4b71Sopenharmony_ci      .padding(20)
108e41f4b71Sopenharmony_ci      .border({ width: 3 })
109e41f4b71Sopenharmony_ci      .margin(50)
110e41f4b71Sopenharmony_ci      .translate({ x: this.offsetX, y: this.offsetY, z: 0 }) // 以组件左上角为坐标原点进行移动
111e41f4b71Sopenharmony_ci      // 左右拖动触发该手势事件
112e41f4b71Sopenharmony_ci      .gesture(
113e41f4b71Sopenharmony_ci      PanGesture(this.panOption)
114e41f4b71Sopenharmony_ci        .onActionStart((event: GestureEvent) => {
115e41f4b71Sopenharmony_ci          console.info('Pan start')
116e41f4b71Sopenharmony_ci        })
117e41f4b71Sopenharmony_ci        .onActionUpdate((event: GestureEvent) => {
118e41f4b71Sopenharmony_ci          if (event) {
119e41f4b71Sopenharmony_ci            this.offsetX = this.positionX + event.offsetX
120e41f4b71Sopenharmony_ci            this.offsetY = this.positionY + event.offsetY
121e41f4b71Sopenharmony_ci          }
122e41f4b71Sopenharmony_ci        })
123e41f4b71Sopenharmony_ci        .onActionEnd((event: GestureEvent) => {
124e41f4b71Sopenharmony_ci          this.positionX = this.offsetX
125e41f4b71Sopenharmony_ci          this.positionY = this.offsetY
126e41f4b71Sopenharmony_ci          console.info('Pan end')
127e41f4b71Sopenharmony_ci        })
128e41f4b71Sopenharmony_ci      )
129e41f4b71Sopenharmony_ci
130e41f4b71Sopenharmony_ci      Button('修改PanGesture触发条件')
131e41f4b71Sopenharmony_ci        .onClick(() => {
132e41f4b71Sopenharmony_ci          // 将PanGesture手势事件触发条件改为双指以任意方向拖动
133e41f4b71Sopenharmony_ci          this.panOption.setDirection(PanDirection.All)
134e41f4b71Sopenharmony_ci          this.panOption.setFingers(2)
135e41f4b71Sopenharmony_ci        })
136e41f4b71Sopenharmony_ci    }
137e41f4b71Sopenharmony_ci  }
138e41f4b71Sopenharmony_ci}
139e41f4b71Sopenharmony_ci```
140e41f4b71Sopenharmony_ci
141e41f4b71Sopenharmony_ci示意图:
142e41f4b71Sopenharmony_ci
143e41f4b71Sopenharmony_ci向左拖动:
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_ci![zh-cn_image_0000001174264374](figures/zh-cn_image_0000001174264374.png) 
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ci点击按钮修改PanGesture触发条件,双指向左下方拖动:
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ci ![zh-cn_image1_0000001174264374](figures/zh-cn_image1_0000001174264374.png)