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

E
ester.zhou 已提交
3
**PinchGesture** is used to trigger a pinch gesture, which requires two to five fingers with a minimum 3 vp distance between the fingers.
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5
>  **NOTE**
E
ester.zhou 已提交
6
>
E
ester.zhou 已提交
7
>  This gesture is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9 10 11

## APIs

E
ester.zhou 已提交
12
PinchGesture(value?: { fingers?: number, distance?: number })
Z
zengyawen 已提交
13

E
ester.zhou 已提交
14 15 16 17 18 19
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| fingers | number | No| Minimum number of fingers to trigger a pinch. The value ranges from 2 to 5.<br>Default value: **2**|
| distance | number | No| Minimum recognition distance, in vp.<br>Default value: **3.0**|
Z
zengyawen 已提交
20 21 22 23


## Events

E
ester.zhou 已提交
24
| Name| Description|
Z
zengyawen 已提交
25
| -------- | -------- |
E
ester.zhou 已提交
26 27 28 29
| onActionStart(event:(event?: [GestureEvent](ts-gesture-settings.md)) =&gt; void) | Triggered when a pinch gesture is recognized.|
| onActionUpdate(event:(event?: [GestureEvent](ts-gesture-settings.md)) =&gt; void) | Triggered when the user moves the finger in a pinch gesture on the screen.|
| onActionEnd(event:(event?: [GestureEvent](ts-gesture-settings.md)) =&gt; void) | Triggered when the finger used for a pinch gesture is lift.|
| onActionCancel(event: () =&gt; void) | Triggered when a tap cancellation event is received after a pinch gesture is recognized.|
Z
zengyawen 已提交
30 31 32 33


## Example

E
ester.zhou 已提交
34 35
```ts
// xxx.ets
Z
zengyawen 已提交
36 37 38
@Entry
@Component
struct PinchGestureExample {
E
ester.zhou 已提交
39
  @State scaleValue: number = 1
Z
zengyawen 已提交
40 41 42 43 44 45

  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)
E
ester.zhou 已提交
46
    .scale({ x: this.scaleValue, y: this.scaleValue, z: this.scaleValue })
Z
zengyawen 已提交
47 48
    .gesture(
      PinchGesture()
E
ester.zhou 已提交
49
        .onActionStart((event: GestureEvent) => {
Z
zengyawen 已提交
50 51
          console.info('Pinch start')
        })
E
ester.zhou 已提交
52
        .onActionUpdate((event: GestureEvent) => {
E
ester.zhou 已提交
53
          this.scaleValue = event.scale
Z
zengyawen 已提交
54 55 56 57 58 59 60 61 62
        })
        .onActionEnd(() => {
          console.info('Pinch end')
        })
    )
  }
}
```

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