ts-gesture-settings.md 7.4 KB
Newer Older
E
ester.zhou 已提交
1
# Gesture Binding
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
Bind different types of gesture events to components and set response methods for them.
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5 6 7
>  **NOTE**
>
>  The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8 9 10


## Required Permissions
Z
zengyawen 已提交
11 12 13

None

Z
zengyawen 已提交
14 15 16 17 18 19 20

## Binding Gesture Recognition


Use the following attributes to bind gesture recognition to a component. When a gesture is recognized, the event callback is invoked to notify the component.


E
ester.zhou 已提交
21
| Parameter| Type| Default Value| Description|
Z
zengyawen 已提交
22
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
23 24 25
| gesture | gesture: GestureType,<br>mask?: GestureMask | gesture: -,<br>mask: GestureMask.Normal | Gesture to recognize.<br>- **gesture**: type of the gesture to bind.<br>- **mask**: event response setting.|
| priorityGesture | gesture: GestureType,<br>mask?: GestureMask | gesture: -,<br>mask: GestureMask.Normal | Gesture to preferentially recognize.<br>- **gesture**: type of the gesture to bind.<br>- **mask**: event response setting.<br>By default, the child component takes precedence over the parent component in gesture recognition. When **priorityGesture** is configured for the parent component, the parent component takes precedence over the child component in gesture recognition.|
| parallelGesture | gesture: GestureType,<br>mask?: GestureMask | gesture: -,<br>mask: GestureMask.Normal | Gesture that can be triggered together with the child component gesture.<br>- **gesture**: type of the gesture to bind. <br>- **mask**: event response setting.<br>The gesture event is not a bubbling event. When **parallelGesture** is set for the parent component, gesture events that are the same for the parent component and child components can be triggered, thereby implementing a bubbling effect.|
Z
zengyawen 已提交
26 27


E
ester.zhou 已提交
28 29
- GestureMask
  | Name| Description|
Z
zengyawen 已提交
30
  | -------- | -------- |
E
ester.zhou 已提交
31
  | Normal | The gestures of child components are not masked and are recognized based on the default gesture recognition sequence.|
32
  | IgnoreInternal | The gestures of child components are masked. Only the gestures of the current component are recognized.<br>However, the built-in gestures of the child components are not masked. For example, when the child component is a **\<List>** component, the built-in sliding gestures can still be triggered.|
Z
zengyawen 已提交
33 34


E
ester.zhou 已提交
35 36
- GestureType
  | Name| Description|
Z
zengyawen 已提交
37
  | -------- | -------- |
E
ester.zhou 已提交
38 39 40 41 42 43 44
  | TapGesture | Tap gesture, which can be a single-tap or multi-tap gesture.|
  | LongPressGesture | Long press gesture.|
  | PanGesture | Pan gesture, which requires a minimum 5 vp movement distance of a finger on the screen.|
  | PinchGesture | Pinch gesture.|
  | RotationGesture | Rotation gesture.|
  | SwipeGesture | Swipe gesture, which can be recognized when the swipe speed is 100 vp/s or higher.|
  | GestureGroup | A group of gestures. Continuous recognition, parallel recognition, and exclusive recognition are supported.|
Z
zengyawen 已提交
45 46 47 48


## Gesture Response Event

E
ester.zhou 已提交
49
The component uses the **gesture** method to bind the gesture object and uses the events provided in this object to respond to the gesture operation. For example, the **onAction** event of the **TapGesture** object can be used to respond to a click event. For details about the event definitions of other gestures, see the corresponding gesture sections.
Z
zengyawen 已提交
50

E
ester.zhou 已提交
51 52
- TapGesture
  | Name| Description|
Z
zengyawen 已提交
53
  | -------- | -------- |
E
ester.zhou 已提交
54
  | onAction((event?:GestureEvent) =&gt; void) | Callback invoked when a tap gesture is recognized.|
Z
zengyawen 已提交
55 56

- GestureEvent attributes
E
ester.zhou 已提交
57
  | Name| Type| Description|
Z
zengyawen 已提交
58
  | -------- | -------- | -------- |
E
ester.zhou 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  | timestamp<sup>8+</sup> | number | Timestamp of the event. It is interval between the time when the event is triggered and the time when the system starts, in nanoseconds.|
  | target<sup>8+</sup> | [EventTarget](ts-universal-events-click.md) | Display area of the element that triggers the gesture event.|
  | source<sup>8+</sup> | SourceType | Event input device.|
  | repeat | boolean | Whether the event is triggered repeatedly. This parameter is used for the **LongPressGesture** event.|
  | fingerList<sup>8+</sup> | FingerInfo[] | Information about all fingers that trigger the event, which is used for the **LongPressGesture** and **TapGesture** events.|
  | offsetX | number | Offset of the gesture event on the x-axis, in vp. This parameter is used for the **PanGesture** event.|
  | offsetY | number | Offset of the gesture event on the y-axis, in vp. This parameter is used for the **PanGesture** event.|
  | angle | number | Rotation angle for the **RotationGesture** event; angle of the swipe gesture for the **SwipeGesture** event, that is, the change in the included angle between the line segment created by the two fingers and the horizontal direction.<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 by using the following formula: 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. |
  | speed<sup>8+</sup> | number | Swipe gesture speed, that is, the average swipe speed of all fingers. The unit is vp/s. This attribute is used for the **SwipeGesture** event.|
  | scale | number | Scale ratio. This attribute is used for the **PinchGesture** event.|
  | pinchCenterX | number | X-coordinate of the center of the pinch gesture, in px. This attribute is used for the **PinchGesture** event.|
  | pinchCenterY | number | Y-coordinate of the center of the pinch gesture, in px. This attribute is used for the **PinchGesture** event.|
  
- SourceType
  | Name| Description|
  | -------- | -------- |
  | Unknown | Unknown device type.|
  | Mouse | Mouse.|
  | TouchScreen | Touchscreen.|

- FingerInfo
  | Name| Type| Description|
  | -------- | -------- | -------- |
  | id | number | Index of a finger.|
  | globalX | number | X-coordinate relative to the upper left corner of the application window.|
  | globalY | number | Y-coordinate relative to the upper left corner of the application window.|
  | localX | number | X-coordinate relative to the upper left corner of the current component.|
  | localY | number | Y-coordinate relative to the upper left corner of the current component.|
Z
zengyawen 已提交
87 88 89 90


## Example

Mr-YX's avatar
Mr-YX 已提交
91
```ts
E
ester.zhou 已提交
92
// xxx.ets
Z
zengyawen 已提交
93 94 95 96 97 98
@Entry
@Component
struct GestureSettingsExample {
  @State value: string = ''

  build() {
Z
zengyawen 已提交
99 100 101 102 103 104 105 106 107
    Column(){
      Column() {
        Text('Click\n' + this.value)
          .gesture(
          TapGesture()
            .onAction(() => {
              this.value = 'gesture onAction'
            }))
      }.height(200).width(300).padding(60).border({ width: 1 })
E
ester.zhou 已提交
108
      // When priorityGesture is specified, the bound gesture is preferentially recognized and the child component gesture is ignored.
Z
zengyawen 已提交
109
      .priorityGesture(
Z
zengyawen 已提交
110
      TapGesture()
Z
zengyawen 已提交
111
        .onAction((event: GestureEvent) => {
Z
zengyawen 已提交
112
          this.value = 'priorityGesture onAction' + '\ncomponent globalPos:('
E
ester.zhou 已提交
113
          + event.target.area.globalPosition.x + ',' + event.target.area.globalPosition.y + ')\nwidth:'
Z
zengyawen 已提交
114
          + event.target.area.width + '\nheight:' + event.target.area.height
Z
zengyawen 已提交
115
        }), GestureMask.IgnoreInternal
Z
zengyawen 已提交
116 117
      )
    }.padding(60)
Z
zengyawen 已提交
118 119 120 121
  }
}
```

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