ts-basic-gestures-pinchgesture.md 2.3 KB
Newer Older
Z
zengyawen 已提交
1
# PinchGesture
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

PinchGesture(options?: { fingers?: number, distance?: number })

- Parameters
E
ester.zhou 已提交
18
    | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
19
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
20 21
  | fingers | number | No | 2 | Minimum number of fingers to trigger a pinch. The value ranges from 2 to 5. |
  | distance | number | No | 3.0 | Minimum recognition distance, in vp. |
Z
zengyawen 已提交
22 23 24 25


## Events

E
ester.zhou 已提交
26
| Name | Description |
Z
zengyawen 已提交
27
| -------- | -------- |
E
ester.zhou 已提交
28 29 30 31
| onActionStart((event?: GestureEvent) => void) | Callback invoked when a pinch gesture is recognized. |
| onActionUpdate((event?: GestureEvent) => void) | Callback invoked during the movement of a pinch gesture. |
| onActionEnd((event?: GestureEvent) => void) | Callback invoked when the finger used for a pinch gesture is lift. |
| onActionCancel(event: () => void) | Callback invoked when a tap cancellation event is received after a pinch gesture is recognized. |
Z
zengyawen 已提交
32

E
ester.zhou 已提交
33 34
- GestureEvent attributes related to the pinch gesture  
    | Name | Type | Description |
Z
zengyawen 已提交
35
  | -------- | -------- | -------- |
E
ester.zhou 已提交
36 37 38
  | scale | number | Scale ratio. This attribute is used for the pinch gesture. |
  | pinchCenterX | number | X-coordinate of the center of the pinch gesture, in px. |
  | pinchCenterY | number | Y-coordinate of the center of the pinch gesture, in px. |
Z
zengyawen 已提交
39 40 41 42


## Example

Z
zengyawen 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

```
@Entry
@Component
struct PinchGestureExample {
  @State scale: number = 1

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
      Text('PinchGesture scale:' + this.scale)
    }
    .height(100).width(200).padding(20).border({ width: 1 }).margin(80)
    .scale({ x: this.scale, y: this.scale, z: this.scale })
    .gesture(
      PinchGesture()
E
ester.zhou 已提交
58
        .onActionStart((event: GestureEvent) => {
Z
zengyawen 已提交
59 60
          console.info('Pinch start')
        })
E
ester.zhou 已提交
61
        .onActionUpdate((event: GestureEvent) => {
Z
zengyawen 已提交
62 63 64 65 66 67 68 69 70 71
          this.scale = event.scale
        })
        .onActionEnd(() => {
          console.info('Pinch end')
        })
    )
  }
}
```

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