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. 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  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 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 (&) 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 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>An inappropriate value can lead to slow response or lagging. 190 191 192## Pinch Gesture 193 194 195```ts 196PinchGesture(value?:{fingers?:number, distance?:number}) 197``` 198 199 200Triggers a pinch gesture. This API has two optional parameters: 201 202 203- **fingers**: minimum number of fingers required for gesture recognition. The value ranges from 2 to 5. The default value is **2**. 204 205- **distance**: minimum distance between fingers required for gesture recognition, in vp. The default value is **5**. 206 207 208The 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. 209 210 211 212```ts 213// xxx.ets 214@Entry 215@Component 216struct Index { 217 @State scaleValue: number = 1; 218 @State pinchValue: number = 1; 219 @State pinchX: number = 0; 220 @State pinchY: number = 0; 221 222 build() { 223 Column() { 224 Column() { 225 Text('PinchGesture scale:\n' + this.scaleValue) 226 Text('PinchGesture center:\n(' + this.pinchX + ',' + this.pinchY + ')') 227 } 228 .height(200) 229 .width(300) 230 .border({ width: 3 }) 231 .margin({ top: 100 }) 232 // Bind the scale factor to the component so that it is scaled by changing the scale factor. 233 .scale({ x: this.scaleValue, y: this.scaleValue, z: 1 }) 234 .gesture( 235 // Bind a three-finger pinch gesture to the component. 236 PinchGesture({ fingers: 3 }) 237 .onActionStart((event: GestureEvent|undefined) => { 238 console.info('Pinch start'); 239 }) 240 // When the pinch gesture is triggered, the callback can be used to obtain the scale factor. 241 .onActionUpdate((event: GestureEvent|undefined) => { 242 if(event){ 243 this.scaleValue = this.pinchValue * event.scale; 244 this.pinchX = event.pinchCenterX; 245 this.pinchY = event.pinchCenterY; 246 } 247 }) 248 .onActionEnd(() => { 249 this.pinchValue = this.scaleValue; 250 console.info('Pinch end'); 251 }) 252 ) 253 } 254 } 255} 256``` 257 258 259 260 261 262## Rotation Gesture 263 264 265```ts 266RotationGesture(value?:{fingers?:number, angle?:number}) 267``` 268 269 270Triggers a rotation gesture. This API has two optional parameters: 271 272 273- **fingers**: minimum number of fingers required for gesture recognition. The value ranges from 2 to 5. The default value is **2**. 274 275- **angle**: minimum angle of rotation required for gesture recognition, in deg. The default value is **1**. 276 277 278The 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. 279 280 281 282```ts 283// xxx.ets 284@Entry 285@Component 286struct Index { 287 @State angle: number = 0; 288 @State rotateValue: number = 0; 289 290 build() { 291 Column() { 292 Text('RotationGesture angle:' + this.angle).fontSize(28) 293 // Bind the rotation to the component so that it is rotated by changing the rotation angle. 294 .rotate({ angle: this.angle }) 295 .gesture( 296 RotationGesture() 297 .onActionStart((event: GestureEvent|undefined) => { 298 console.info('RotationGesture is onActionStart'); 299 }) 300 // When the rotation gesture takes effect, the rotation angle is obtained through the callback and the component is rotated accordingly. 301 .onActionUpdate((event: GestureEvent|undefined) => { 302 if(event){ 303 this.angle = this.rotateValue + event.angle; 304 } 305 console.info('RotationGesture is onActionEnd'); 306 }) 307 // When the fingers lift from the screen, the component is fixed at the angle where rotation ends. 308 .onActionEnd(() => { 309 this.rotateValue = this.angle; 310 console.info('RotationGesture is onActionEnd'); 311 }) 312 .onActionCancel(() => { 313 console.info('RotationGesture is onActionCancel'); 314 }) 315 ) 316 .height(200) 317 .width(300) 318 .padding(20) 319 .border({ width: 3 }) 320 .margin(100) 321 } 322 } 323} 324``` 325 326 327 328 329 330## Swipe Gesture 331 332 333```ts 334SwipeGesture(value?:{fingers?:number, direction?:SwipeDirection, speed?:number}) 335``` 336 337 338Triggers a swipe gesture, which can be recognized when the swipe speed is 100 vp/s or higher. This API has three optional parameters: 339 340 341- **fingers**: minimum number of fingers required for gesture recognition. The value ranges from 1 to 10. The default value is **1**. 342 343- **direction**: swipe direction. The enumerated value supports the AND (&) and OR (\|) operations. The default value is **SwipeDirection.All**. 344 345- **speed**: minimum speed of the swipe gesture, in vp/s. The default value is **100**. 346 347 348The following exemplifies how to bind a swipe gesture to the **Column** component to rotate the component: 349 350 351 352```ts 353// xxx.ets 354@Entry 355@Component 356struct Index { 357 @State rotateAngle: number = 0; 358 @State speed: number = 1; 359 360 build() { 361 Column() { 362 Column() { 363 Text("SwipeGesture speed\n" + this.speed) 364 Text("SwipeGesture angle\n" + this.rotateAngle) 365 } 366 .border({ width: 3 }) 367 .width(300) 368 .height(200) 369 .margin(100) 370 // Bind rotation to the Column component and change the rotation angle through the swipe speed and angle. 371 .rotate({ angle: this.rotateAngle }) 372 .gesture( 373 // Bind to the component the swipe gesture that can be triggered only when the user swipes in the vertical direction. 374 SwipeGesture({ direction: SwipeDirection.Vertical }) 375 // When the swipe gesture is triggered, the swipe speed and angle are obtained, which can be used to modify the layout parameters. 376 .onAction((event: GestureEvent|undefined) => { 377 if(event){ 378 this.speed = event.speed; 379 this.rotateAngle = event.angle; 380 } 381 }) 382 ) 383 } 384 } 385} 386``` 387 388 389 390 391 392>**NOTE** 393> 394>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**. 395