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