ts-basic-gestures-swipegesture.md 2.8 KB
Newer Older
Z
zengyawen 已提交
1
# SwipeGesture
Z
zengyawen 已提交
2

3
**\<SwipeGesture>** is used to implement a swipe gesture, which can be recognized when the swipe speed is 100 vp/s or higher.
Z
zengyawen 已提交
4

5 6
> **NOTE**
>
Z
zengyawen 已提交
7 8 9 10
> 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 已提交
11 12 13

None

Z
zengyawen 已提交
14 15 16 17 18 19

## APIs

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

- Parameters
20
  | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
21
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
22 23 24
  | 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 已提交
25 26

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


## Events

E
ester.zhou 已提交
36
| Name | Description |
Z
zengyawen 已提交
37
| -------- | -------- |
38
| onAction(callback:(event?: GestureEvent) =&gt; void) | Invoked when a swipe gesture is recognized. |
Z
zengyawen 已提交
39 40


E
ester.zhou 已提交
41
- GestureEvent attributes related to the swipe gesture  
42
  | Name | Type | Description |
Z
zengyawen 已提交
43
  | -------- | -------- | -------- |
44
  | angle | number | Angle of the swipe gesture.<br/>**NOTE**<br/>Angle calculation method: After a swipe gesture is recognized, 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 已提交
45
  | speed | number | Speed of the swipe gesture. |
Z
zengyawen 已提交
46

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

## Example

Z
zengyawen 已提交
51 52 53 54 55 56 57 58 59 60

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

  build() {
    Column() {
Z
zengyawen 已提交
61 62
      Text("SwipGesture speed : " + this.speed)
      Text("SwipGesture angle : " + this.rotateAngle)
Z
zengyawen 已提交
63 64 65 66 67 68 69
    }
    .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 已提交
70
        .onAction((event: GestureEvent) => {
Z
zengyawen 已提交
71 72 73 74 75 76 77 78
          this.speed = event.speed
          this.rotateAngle = event.angle
      })
    )
  }
}
```

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