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

Z
zengyawen 已提交
3 4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
Z
zengyawen 已提交
5

Z
zengyawen 已提交
6 7

## 权限列表
Z
zengyawen 已提交
8 9


Z
zengyawen 已提交
10

Z
zengyawen 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

## 接口

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

- 参数
  | 参数名称 | 参数类型 | 必填 | 默认值 | 参数描述 |
  | -------- | -------- | -------- | -------- | -------- |
  | fingers | number | 否 | 1 | 触发滑动的最少手指数,最小为1指, 最大取值为10指。 |
  | direction | PanDirection | 否 | All | 设置滑动方向,此枚举值支持逻辑与(&)和逻辑或(\|)运算。 |
  | distance | number | 否 | 5.0 | 最小滑动识别距离,单位为vp。 |

- PanDirection枚举说明
  | 名称 | 描述 | 
  | -------- | -------- |
  | All | 所有方向可滑动。 | 
  | Horizontal | 水平方向可滑动。 | 
  | Vertical | 竖直方向可滑动。 | 
  | Left | 向左滑动。 | 
  | Right | 向右滑动。 | 
  | Up | 向上滑动。 | 
  | Down | 向下滑动。 | 
  | None | 任何方向都不可滑动。 | 


### PanGestureOption
Z
zengyawen 已提交
37 38 39

通过PanGestureOption对象接口可以动态修改滑动手势识别器的属性,从而避免通过状态变量修改属性(状态变量修改会导致UI刷新)。

Z
zengyawen 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
PanGestureOption(options?: { fingers?: number, direction?: PanDirection, distance?: number })

- 参数
  同PanGesture参数说明。

- 接口
  | 名称 | 功能描述 | 
  | -------- | -------- |
  | setDirection(value: PanDirection) | 设置direction属性。 | 
  | setDistance(value: number) | 设置distance属性。 | 
  | setFingers(value: number) | 设置fingers属性。 | 


## 事件

| 名称 | 功能描述 | 
56
| -------- | -------- | 
Z
zengyawen 已提交
57 58 59 60 61
| onActionStart(callback: (event?: PanGestureEvent) => void) | Pan手势识别成功回调。 | 
| onActionUpdate(callback: (event?: PanGestureEvent) => void) | Pan手势移动过程中回调。 | 
| onActionEnd(callback: (event?: PanGestureEvent) => void) | Pan手势识别成功,手指抬起后触发回调。 | 
| onActionCancel(callback: () => void) | Pan手势识别成功,接收到触摸取消事件触发回调。 | 

62
- PanGestureEvent<sup>8+</sup>对象说明
Z
zengyawen 已提交
63 64 65 66 67 68 69
  | 属性名称 | 属性类型 | 描述 | 
  | -------- | -------- | -------- |
  | offsetX | number | 手势事件偏移量,单位为vp。 | 
  | offsetY | number | 手势事件偏移量,单位为vp。 | 


## 示例
Z
zengyawen 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

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

Z
zengyawen 已提交
101
![zh-cn_image_0000001174264374](figures/zh-cn_image_0000001174264374.gif)