• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Single Gesture
2
3
4## Tap Gesture
5
6
7```ts
8TapGesture(value?:{count?:number; fingers?:number})
9```
10
11
12Triggers a tap gesture with one or more taps. This API has two optional parameters:
13
14
15- **count**: number of consecutive taps required for gesture recognition. The default value is **1**. A value less than 1 evaluates to the default value **1**. If there are multiple taps, the timeout interval between a lift and the next tap is 300 ms.
16
17- **fingers**: number of fingers required for gesture recognition. The value ranges from 1 to 10. The default value is **1**. If the number of fingers used is less than the specified one within 300 ms after the first finger is tapped, the gesture fails to be recognized. Gesture recognition also fails if the number of fingers used for the tap exceeds the value of **fingers**.
18    The following example binds a double-tap gesture (a tap gesture whose **count** value is **2**) to the **\<Text>** component:
19
20  ```ts
21  // xxx.ets
22  @Entry
23  @Component
24  struct Index {
25    @State value: string = "";
26
27    build() {
28      Column() {
29        Text('Click twice').fontSize(28)
30          .gesture(
31            // Bind a tap gesture whose count value is 2.
32            TapGesture({ count: 2 })
33              .onAction((event: GestureEvent|undefined) => {
34              if(event){
35                this.value = JSON.stringify(event.fingerList[0]);
36              }
37              }))
38        Text(this.value)
39      }
40      .height(200)
41      .width(250)
42      .padding(20)
43      .border({ width: 3 })
44      .margin(30)
45    }
46  }
47  ```
48
49  ![tap](figures/tap.gif)
50
51
52## Long Press Gesture
53
54
55```ts
56LongPressGesture(value?:{fingers?:number; repeat?:boolean; duration?:number})
57```
58
59
60Triggers a long press gesture. This API has three optional parameters:
61
62
63- **fingers**: minimum number of fingers required for gesture recognition. The value ranges from 1 to 10. The default value is **1**.
64
65- **repeat**: whether to continuously trigger the event callback. The default value is **false**.
66
67- **duration**: minimum hold-down time, in ms. The default value is **500**.
68
69
70The following exemplifies how to bind a long press gesture that can be repeatedly triggered to the **\<Text>** component:
71
72
73
74```ts
75// xxx.ets
76@Entry
77@Component
78struct Index {
79  @State count: number = 0;
80
81  build() {
82    Column() {
83      Text('LongPress OnAction:' + this.count).fontSize(28)
84        .gesture(
85          // Bind a long press gesture that can be triggered repeatedly.
86          LongPressGesture({ repeat: true })
87           .onAction((event: GestureEvent|undefined) => {
88              if(event){
89                if (event.repeat) {
90                  this.count++;
91                }
92              }
93            })
94            .onActionEnd(() => {
95              this.count = 0;
96            })
97        )
98    }
99    .height(200)
100    .width(250)
101    .padding(20)
102    .border({ width: 3 })
103    .margin(30)
104  }
105}
106```
107
108
109![longPress](figures/longPress.gif)
110
111
112## Pan Gesture
113
114
115```ts
116PanGesture(value?:{ fingers?:number; direction?:PanDirection; distance?:number})
117```
118
119
120Triggers a pan gesture, which requires the minimum movement distance (5 vp by default) of a finger on the screen. This API has three optional parameters:
121
122
123- **fingers**: minimum number of fingers required for gesture recognition. The value ranges from 1 to 10. The default value is **1**.
124
125- **direction**: pan direction. The enumerated value supports the AND (&amp;) and OR (\|) operations. The default value is **Pandirection.All.**
126
127- **distance**: minimum amount of finger movement required for gesture recognition, in vp. The default value is **5**.
128
129
130The following exemplifies how to bind a pan gesture to the **\<Text>** component. You can pan a component by modifying the layout and position information of the component in the **PanGesture** callback.
131
132
133
134```ts
135// xxx.ets
136@Entry
137@Component
138struct Index {
139  @State offsetX: number = 0;
140  @State offsetY: number = 0;
141  @State positionX: number = 0;
142  @State positionY: number = 0;
143
144  build() {
145    Column() {
146      Text('PanGesture Offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY)
147        .fontSize(28)
148        .height(200)
149        .width(300)
150        .padding(20)
151        .border({ width: 3 })
152          // Bind the layout and position information to the component.
153        .translate({ x: this.offsetX, y: this.offsetY, z: 0 })
154        .gesture(
155          // Bind a pan gesture to the component.
156          PanGesture()
157           .onActionStart((event: GestureEvent|undefined) => {
158              console.info('Pan start');
159            })
160              // When the pan gesture is recognized, modify the layout and position information of the component based on the callback.
161            .onActionUpdate((event: GestureEvent|undefined) => {
162              if(event){
163                this.offsetX = this.positionX + event.offsetX;
164                this.offsetY = this.positionY + event.offsetY;
165              }
166            })
167            .onActionEnd(() => {
168              this.positionX = this.offsetX;
169              this.positionY = this.offsetY;
170            })
171        )
172    }
173    .height(200)
174    .width(250)
175  }
176}
177```
178
179
180![pan](figures/pan.gif)
181
182
183>**NOTE**
184>
185>Most swipeable components, such as **\<List>**, **\<Grid>**, **\<Scroll>**, and **\<Tab>**, allow for swiping through the pan gesture. If you bind the pan gesture or [swipe gesture](#swipe-gesture) to a child of these components, competition for gesture recognition will result.
186>
187>If the pan gesture is bound to a child component, the component, instead of its parent, responds to the pan gestures recognized. If you want the parent component to respond, you need to modify the gesture binding method or transfer messages from the child component to the parent component, or modify the **distance** parameters in **PanGesture** for the components. If the swipe gesture is bound to a child component, to allow the parent component to respond to gestures, you need to modify the parameters of **PanGesture** and **SwipeGesture**, since the swipe gesture and pan gesture are recognized with different conditions.
188
189
190## Pinch Gesture
191
192
193```ts
194PinchGesture(value?:{fingers?:number; distance?:number})
195```
196
197
198Triggers a pinch gesture. This API has two optional parameters:
199
200
201- **fingers**: minimum number of fingers required for gesture recognition. The value ranges from 2 to 5. The default value is **2**.
202
203- **distance**: minimum distance between fingers required for gesture recognition, in vp. The default value is **5**.
204
205
206The following exemplifies how to bind a three-finger pinch gesture to the **\<Column>** component. You can obtain the scale factor from the callback of **PinchGesture** to scale the component.
207
208
209
210```ts
211// xxx.ets
212@Entry
213@Component
214struct Index {
215  @State scaleValue: number = 1;
216  @State pinchValue: number = 1;
217  @State pinchX: number = 0;
218  @State pinchY: number = 0;
219
220  build() {
221    Column() {
222      Column() {
223        Text('PinchGesture scale:\n' + this.scaleValue)
224        Text('PinchGesture center:\n(' + this.pinchX + ',' + this.pinchY + ')')
225      }
226      .height(200)
227      .width(300)
228      .border({ width: 3 })
229      .margin({ top: 100 })
230      // Bind the scale factor to the component so that it is scaled by changing the scale factor.
231      .scale({ x: this.scaleValue, y: this.scaleValue, z: 1 })
232      .gesture(
233        // Bind a three-finger pinch gesture to the component.
234        PinchGesture({ fingers: 3 })
235          .onActionStart((event: GestureEvent|undefined) => {
236            console.info('Pinch start');
237          })
238            // When the pinch gesture is triggered, the callback can be used to obtain the scale factor.
239          .onActionUpdate((event: GestureEvent|undefined) => {
240            if(event){
241              this.scaleValue = this.pinchValue * event.scale;
242              this.pinchX = event.pinchCenterX;
243              this.pinchY = event.pinchCenterY;
244            }
245          })
246          .onActionEnd(() => {
247            this.pinchValue = this.scaleValue;
248            console.info('Pinch end');
249          })
250      )
251    }
252  }
253}
254```
255
256
257![pinch](figures/pinch.png)
258
259
260## Rotation Gesture
261
262
263```ts
264RotationGesture(value?:{fingers?:number; angle?:number})
265```
266
267
268Triggers a rotation gesture. This API has two optional parameters:
269
270
271- **fingers**: minimum number of fingers required for gesture recognition. The value ranges from 2 to 5. The default value is **2**.
272
273- **angle**: minimum angle of rotation required for gesture recognition, in deg. The default value is **1**.
274
275
276The following exemplifies how to bind a rotation gesture to the **\<Text>** component. You can obtain the rotation angle from the callback of **RotationGesture** and implement rotation on the component.
277
278
279
280```ts
281// xxx.ets
282@Entry
283@Component
284struct Index {
285  @State angle: number = 0;
286  @State rotateValue: number = 0;
287
288  build() {
289    Column() {
290      Text('RotationGesture angle:' + this.angle).fontSize(28)
291        // Bind the rotation to the component so that it is rotated by changing the rotation angle.
292        .rotate({ angle: this.angle })
293        .gesture(
294          RotationGesture()
295           .onActionStart((event: GestureEvent|undefined) => {
296              console.info('RotationGesture is onActionStart');
297            })
298              // When the rotation gesture takes effect, the rotation angle is obtained through the callback and the component is rotated accordingly.
299            .onActionUpdate((event: GestureEvent|undefined) => {
300              if(event){
301                this.angle = this.rotateValue + event.angle;
302              }
303              console.info('RotationGesture is onActionEnd');
304            })
305              // When the fingers lift from the screen, the component is fixed at the angle where rotation ends.
306            .onActionEnd(() => {
307              this.rotateValue = this.angle;
308              console.info('RotationGesture is onActionEnd');
309            })
310            .onActionCancel(() => {
311              console.info('RotationGesture is onActionCancel');
312            })
313        )
314        .height(200)
315        .width(300)
316        .padding(20)
317        .border({ width: 3 })
318        .margin(100)
319    }
320  }
321}
322```
323
324
325![rotation](figures/rotation.png)
326
327
328## Swipe Gesture
329
330
331```ts
332SwipeGesture(value?:{fingers?:number; direction?:SwipeDirection; speed?:number})
333```
334
335
336Triggers a swipe gesture, which can be recognized when the swipe speed is 100 vp/s or higher. This API has three optional parameters:
337
338
339- **fingers**: minimum number of fingers required for gesture recognition. The value ranges from 1 to 10. The default value is **1**.
340
341- **direction**: swipe direction. The enumerated value supports the AND (&amp;) and OR (\|) operations. The default value is **SwipeDirection.All**.
342
343- **speed**: minimum speed of the swipe gesture, in vp/s. The default value is **100**.
344
345
346The following exemplifies how to bind a swipe gesture to the **\<Column>** component to rotate the component:
347
348
349
350```ts
351// xxx.ets
352@Entry
353@Component
354struct Index {
355  @State rotateAngle: number = 0;
356  @State speed: number = 1;
357
358  build() {
359    Column() {
360      Column() {
361        Text("SwipeGesture speed\n" + this.speed)
362        Text("SwipeGesture angle\n" + this.rotateAngle)
363      }
364      .border({ width: 3 })
365      .width(300)
366      .height(200)
367      .margin(100)
368      // Bind rotation to the <Column> component and change the rotation angle through the swipe speed and angle.
369      .rotate({ angle: this.rotateAngle })
370      .gesture(
371        // Bind to the component the swipe gesture that can be triggered only when the user swipes in the vertical direction.
372        SwipeGesture({ direction: SwipeDirection.Vertical })
373          // When the swipe gesture is triggered, the swipe speed and angle are obtained, which can be used to modify the layout parameters.
374          .onAction((event: GestureEvent|undefined) => {
375            if(event){
376              this.speed = event.speed;
377              this.rotateAngle = event.angle;
378            }
379          })
380      )
381    }
382  }
383}
384```
385
386
387![swipe](figures/swipe.gif)
388
389
390>**NOTE**
391>
392>When the swipe gesture and pan gesture are simultaneously bound to a component in default or mutually exclusive mode, competition for gesture recognition occurs. Whichever gesture meets the trigger condition first is recognized. By default, a swipe gesture is recognized when the swipe speed reaches 100 vp/s, and a pan gesture is recognized when the amount of finger movement reaches 5 vp. To allow a specific gesture to be recognized before the other, you can modify the parameter settings in **SwipeGesture** and **PanGesture**.
393