ts-basic-gestures-swipegesture.md 2.8 KB
Newer Older
Z
zengyawen 已提交
1
# SwipeGesture
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 8. Updates will be marked with a superscript to indicate their earliest API version.


## Required Permissions
Z
zengyawen 已提交
9 10 11

None

Z
zengyawen 已提交
12 13 14 15 16 17

## APIs

SwipeGesture(value?: { fingers?: number; direction?: SwipeDirection; speed?: number })

- Parameters
E
ester.zhou 已提交
18
    | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
19
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
20 21 22
  | fingers | number | No | 1 | Minimum number of fingers to trigger a swipe gesture. The value ranges from 1 to 10. |
  | direction | SwipeDirection | No | SwipeDirection.All | Swipe direction. |
  | speed | number | No | 100 | Minimum speed of the swipe gesture (100 vp/s). |
Z
zengyawen 已提交
23 24

- SwipeDirection enums
E
ester.zhou 已提交
25
    | Name | Description |
Z
zengyawen 已提交
26
  | -------- | -------- |
E
ester.zhou 已提交
27 28 29
  | All | All directions. |
  | Horizontal | Horizontal direction. |
  | Vertical | Vertical direction. |
Z
zengyawen 已提交
30 31 32 33


## Events

E
ester.zhou 已提交
34
| Name | Description |
Z
zengyawen 已提交
35
| -------- | -------- |
E
ester.zhou 已提交
36
| onAction(callback:(event?: GestureEvent) => void) | Callback invoked when a swipe gesture is recognized. |
Z
zengyawen 已提交
37 38


E
ester.zhou 已提交
39 40
- GestureEvent attributes related to the swipe gesture  
    | Name | Type | Description |
Z
zengyawen 已提交
41
  | -------- | -------- | -------- |
E
ester.zhou 已提交
42
  | angle | number | Angle of the swipe gesture.<br/>>&nbsp;![icon-note.gif](public_sys-resources/icon-note.gif)&nbsp;**NOTE**<br/>>&nbsp;Angle calculation method: After the swipe gesture is identified, a line connecting the two fingers is identified as the initial line. As the fingers swipe, the line between the fingers rotates. Based on the coordinates of the initial line's and current line's end points, an arc tangent function is used to calculate the respective included angle of the points relative to the horizontal direction. Rotation angle = arctan2(cy2-cy1,cx2-cx1) - arctan2(y2-y1,x2-x1). The initial line is used as the coordinate system. The clockwise rotation is 0 to 180 degrees, and the counter-clockwise rotation is –180 to 0 degrees. |
E
ester.zhou 已提交
43
  | speed | number | Speed of the swipe gesture. |
Z
zengyawen 已提交
44

E
ester.zhou 已提交
45
![en-us_image_0000001231374559](figures/en-us_image_0000001231374661.png)
Z
zengyawen 已提交
46 47 48

## Example

Z
zengyawen 已提交
49 50 51 52 53 54 55 56 57 58

```
@Entry
@Component
struct SwipeGestureExample {
  @State rotateAngle : number = 0
  @State speed : number = 1

  build() {
    Column() {
Z
zengyawen 已提交
59 60
      Text("SwipGesture speed : " + this.speed)
      Text("SwipGesture angle : " + this.rotateAngle)
Z
zengyawen 已提交
61 62 63 64 65 66 67
    }
    .position({x: 80, y: 200})
    .border({width:2})
    .width(260).height(260)
    .rotate({x: 0, y: 0, z: 1, angle: this.rotateAngle})
    .gesture(
      SwipeGesture({fingers: 1, direction:SwipeDirection.Vertical})
E
ester.zhou 已提交
68
        .onAction((event: GestureEvent) => {
Z
zengyawen 已提交
69 70 71 72 73 74 75 76
          this.speed = event.speed
          this.rotateAngle = event.angle
      })
    )
  }
}
```

E
ester.zhou 已提交
77
![en-us_image_0000001231374559](figures/en-us_image_0000001231374559.gif)