1# Single Gesture 2 3 4## TapGesture 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 multi-tap is configured, 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 for the tap 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) => { 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  48 49 50## LongPressGesture 51 52 53```ts 54LongPressGesture(value?:{fingers?:number; repeat?:boolean; duration?:number}) 55``` 56 57 58Triggers a long press gesture, which requires one or more fingers with a minimum 500 ms hold-down time. This API has three optional parameters: 59 60 61- **fingers**: minimum number of fingers required for gesture recognition. The value ranges from 1 to 10. The default value is **1**. 62 63- **repeat**: whether to continuously trigger the event callback. The default value is **false**. 64 65- **duration**: minimum hold-down time, in ms. The default value is **500**. 66 67 68The following exemplifies how to bind a long press 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 long press gesture 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 106 107 108## PanGesture 109 110 111```ts 112PanGestureOptions(value?:{ fingers?:number; direction?:PanDirection; distance?:number}) 113``` 114 115 116Triggers 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: 117 118 119- **fingers**: minimum number of fingers required for gesture recognition. The value ranges from 1 to 10. The default value is **1**. 120 121- **direction**: pan direction. The enumerated value supports the AND (&) and OR (\|) operations. The default value is **Pandirection.All.** 122 123- **distance**: minimum pan distance required for gesture recognition, in vp. The default value is **5**. 124 125 126The 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. 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 and position information to the component. 149 .translate({ x: this.offsetX, y: this.offsetY, z: 0 }) 150 .gesture( 151 // Bind the pan gesture to the component. 152 PanGesture() 153 .onActionStart((event: GestureEvent) => { 154 console.info('Pan start'); 155 }) 156 // When the drag gesture is triggered, modify the layout and position information of the component based on the callback. 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 175 176 177>**NOTE** 178> 179>Most slidable components, such as **\<List>**, **\<Grid>**, **\<Scroll>**, and **\<Tab>**, slide through the pan gesture. Therefore, binding the [pan gesture](#pangesture) or [swipe gesture](#swipegesture) to child components will cause gesture competition. 180> 181>When a child component is bound to the pan gesture, sliding in the child component area triggers only the pan gesture of the child component. 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 panning more sensitive. When a child component is bound to the swipe gesture, 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 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 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. TThe minimum value is 1 and the maximum value is 10. The default value is 1. 327 328- **direction**: swipe direction. 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. 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 swipe gesture is triggered, the swipe speed and angle are obtained, which can be used to modify the layout parameters. 361 .onAction((event: GestureEvent) => { 362 this.speed = event.speed; 363 this.rotateAngle = event.angle; 364 }) 365 ) 366 } 367 } 368} 369``` 370 371 372 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