ts-basic-gestures-pinchgesture.md 2.5 KB
Newer Older
Z
zengyawen 已提交
1
# PinchGesture
Z
zengyawen 已提交
2

T
explain  
tianyu 已提交
3 4
用于触发捏合手势,触发捏合手势的最少手指为2指,最大为5指,最小识别距离为3vp。

H
geshi  
HelloCrease 已提交
5
>  **说明:**
G
gmy 已提交
6 7
>
>  从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9 10 11

## 接口

G
gmy 已提交
12
PinchGesture(value?: { fingers?: number, distance?: number })
Z
zengyawen 已提交
13

G
gmy 已提交
14 15 16 17 18
**参数:**

| 参数名称 | 参数类型 | 必填 | 参数描述 |
| -------- | -------- | -------- | -------- |
| fingers | number | 否 | 触发捏合的最少手指数,&nbsp;最小为2指,最大为5指。<br/>默认值:2 |
19
| distance | number | 否 | 最小识别距离,单位为vp。<br/>默认值:3 |
Z
zengyawen 已提交
20 21 22 23


## 事件

T
explain  
tianyu 已提交
24 25
| 名称 | 功能描述 |
| -------- | -------- |
S
sienna1128 已提交
26 27 28
| onActionStart(event:(event?:&nbsp;[GestureEvent](ts-gesture-settings.md#gestureevent对象说明))&nbsp;=&gt;&nbsp;void) | Pinch手势识别成功回调。 |
| onActionUpdate(event:(event?:&nbsp;[GestureEvent](ts-gesture-settings.md#gestureevent对象说明))&nbsp;=&gt;&nbsp;void) | Pinch手势移动过程中回调。 |
| onActionEnd(event:(event?:&nbsp;[GestureEvent](ts-gesture-settings.md#gestureevent对象说明))&nbsp;=&gt;&nbsp;void) | Pinch手势识别成功,手指抬起后触发回调。 |
T
explain  
tianyu 已提交
29
| onActionCancel(event:&nbsp;()&nbsp;=&gt;&nbsp;void) | Pinch手势识别成功,接收到触摸取消事件触发回调。 |
Z
zengyawen 已提交
30 31 32


## 示例
Z
zengyawen 已提交
33

H
geshi  
HelloCrease 已提交
34 35
```ts
// xxx.ets
Z
zengyawen 已提交
36 37 38
@Entry
@Component
struct PinchGestureExample {
Y
yamila 已提交
39 40 41 42
  @State scaleValue: number = 1
  @State pinchValue: number = 1
  @State pinchX: number = 0
  @State pinchY: number = 0
Z
zengyawen 已提交
43 44

  build() {
45 46 47 48 49 50 51 52 53 54 55 56 57 58
    Column() {
      Column() {
        Text('PinchGesture scale:\n' + this.scaleValue)
        Text('PinchGesture center:\n(' + this.pinchX + ',' + this.pinchY + ')')
      }
      .height(200)
      .width(300)
      .padding(20)
      .border({ width: 3 })
      .margin({ top: 100 })
      .scale({ x: this.scaleValue, y: this.scaleValue, z: 1 })
      // 三指捏合触发该手势事件
      .gesture(
      PinchGesture({ fingers: 3 })
L
liuliu 已提交
59
        .onActionStart((event?: GestureEvent) => {
Y
yamila 已提交
60
          console.info('Pinch start')
Z
zengyawen 已提交
61
        })
L
liuliu 已提交
62 63 64 65 66 67
        .onActionUpdate((event?: GestureEvent) => {
          if (event) {
            this.scaleValue = this.pinchValue * event.scale
            this.pinchX = event.pinchCenterX
            this.pinchY = event.pinchCenterY
          }
Z
zengyawen 已提交
68 69
        })
        .onActionEnd(() => {
Y
yamila 已提交
70 71
          this.pinchValue = this.scaleValue
          console.info('Pinch end')
Z
zengyawen 已提交
72
        })
73 74
      )
    }.width('100%')
Z
zengyawen 已提交
75 76 77 78
  }
}
```

T
tianyu 已提交
79
 ![zh-cn_image_0000001174582848](figures/zh-cn_image_0000001174582848.png)