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


Z
zengyawen 已提交
4 5 6 7 8
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE:**
> 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 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

## APIs

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

- Parameters
    | Name | Type | Mandatory | Default Value | Description | 
  | -------- | -------- | -------- | -------- | -------- |
  | 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. | 


## Events

  | Name | Description | 
| -------- | -------- |
| onActionStart((event?: PinchGestureEvent) => void) | Callback invoked when a pinch gesture is recognized. | 
| onActionUpdate((event?: PinchGestureEvent) => void) | Callback invoked during the movement of a pinch gesture. | 
| onActionEnd((event?: PinchGestureEvent) => 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. | 

- PinchGestureEvent<sup>8+</sup> attributes
    | Name | Type | Description | 
  | -------- | -------- | -------- |
  | scale | number | Scale&nbsp;ratio.&nbsp;This&nbsp;attribute&nbsp;is&nbsp;used&nbsp;for&nbsp;the&nbsp;pinch&nbsp;gesture. | 
  | pinchCenterX | number | X-coordinate&nbsp;of&nbsp;the&nbsp;center&nbsp;of&nbsp;the&nbsp;pinch&nbsp;gesture,&nbsp;in&nbsp;px. | 
  | pinchCenterY | number | Y-coordinate&nbsp;of&nbsp;the&nbsp;center&nbsp;of&nbsp;the&nbsp;pinch&nbsp;gesture,&nbsp;in&nbsp;px. | 


## 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()
Z
zengyawen 已提交
58
        .onActionStart((event: PinchGestureEvent) => {
Z
zengyawen 已提交
59 60
          console.info('Pinch start')
        })
Z
zengyawen 已提交
61
        .onActionUpdate((event: PinchGestureEvent) => {
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)