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  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 116class pgotmp{ 117 fingers?:number = 0 118 direction?:PanDirection|null = null 119 distance?:number = 0 120} 121let pgo:pgotmp = new pgotmp() 122new PanGestureOptions(value?:pgo) 123``` 124 125 126Triggers 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: 127 128 129- **fingers**: minimum number of fingers required for gesture recognition. The value ranges from 1 to 10. The default value is **1**. 130 131- **direction**: pan direction. The enumerated value supports the AND (&) and OR (\|) operations. The default value is **Pandirection.All.** 132 133- **distance**: minimum amount of finger movement required for gesture recognition, in vp. The default value is **5**. 134 135 136The 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. 137 138 139 140```ts 141// xxx.ets 142@Entry 143@Component 144struct Index { 145 @State offsetX: number = 0; 146 @State offsetY: number = 0; 147 @State positionX: number = 0; 148 @State positionY: number = 0; 149 150 build() { 151 Column() { 152 Text('PanGesture Offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY) 153 .fontSize(28) 154 .height(200) 155 .width(300) 156 .padding(20) 157 .border({ width: 3 }) 158 // Bind the layout and position information to the component. 159 .translate({ x: this.offsetX, y: this.offsetY, z: 0 }) 160 .gesture( 161 // Bind a pan gesture to the component. 162 PanGesture() 163 .onActionStart((event: GestureEvent|undefined) => { 164 console.info('Pan start'); 165 }) 166 // When the pan gesture is recognized, modify the layout and position information of the component based on the callback. 167 .onActionUpdate((event: GestureEvent|undefined) => { 168 if(event){ 169 this.offsetX = this.positionX + event.offsetX; 170 this.offsetY = this.positionY + event.offsetY; 171 } 172 }) 173 .onActionEnd(() => { 174 this.positionX = this.offsetX; 175 this.positionY = this.offsetY; 176 }) 177 ) 178 } 179 .height(200) 180 .width(250) 181 } 182} 183``` 184 185 186 187 188 189>**NOTE** 190> 191>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. 192> 193>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. 194 195 196## Pinch Gesture 197 198 199```ts 200PinchGesture(value?:{fingers?:number; distance?:number}) 201``` 202 203 204Triggers a pinch gesture. This API has two optional parameters: 205 206 207- **fingers**: minimum number of fingers required for gesture recognition. The value ranges from 2 to 5. The default value is **2**. 208 209- **distance**: minimum distance between fingers required for gesture recognition, in vp. The default value is **3**. 210 211 212 The 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. 213 214 215 216```ts 217// xxx.ets 218@Entry 219@Component 220struct Index { 221 @State scaleValue: number = 1; 222 @State pinchValue: number = 1; 223 @State pinchX: number = 0; 224 @State pinchY: number = 0; 225 226 build() { 227 Column() { 228 Column() { 229 Text('PinchGesture scale:\n' + this.scaleValue) 230 Text('PinchGesture center:\n(' + this.pinchX + ',' + this.pinchY + ')') 231 } 232 .height(200) 233 .width(300) 234 .border({ width: 3 }) 235 .margin({ top: 100 }) 236 // Bind the scale factor to the component so that it is scaled by changing the scale factor. 237 .scale({ x: this.scaleValue, y: this.scaleValue, z: 1 }) 238 .gesture( 239 // Bind a three-finger pinch gesture to the component. 240 PinchGesture({ fingers: 3 }) 241 .onActionStart((event: GestureEvent|undefined) => { 242 console.info('Pinch start'); 243 }) 244 // When the pinch gesture is triggered, the callback can be used to obtain the scale factor. 245 .onActionUpdate((event: GestureEvent|undefined) => { 246 if(event){ 247 this.scaleValue = this.pinchValue * event.scale; 248 this.pinchX = event.pinchCenterX; 249 this.pinchY = event.pinchCenterY; 250 } 251 }) 252 .onActionEnd(() => { 253 this.pinchValue = this.scaleValue; 254 console.info('Pinch end'); 255 }) 256 ) 257 } 258 } 259} 260``` 261 262 263 264 265 266## Rotation Gesture 267 268 269```ts 270RotationGesture(value?:{fingers?:number; angle?:number}) 271``` 272 273 274Triggers a rotation gesture. This API has two optional parameters: 275 276 277- **fingers**: minimum number of fingers required for gesture recognition. The value ranges from 2 to 5. The default value is **2**. 278 279- **angle**: minimum angle of rotation required for gesture recognition, in deg. The default value is **1**. 280 281 282 The 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. 283 284 285 286```ts 287// xxx.ets 288@Entry 289@Component 290struct Index { 291 @State angle: number = 0; 292 @State rotateValue: number = 0; 293 294 build() { 295 Column() { 296 Text('RotationGesture angle:' + this.angle).fontSize(28) 297 // Bind the rotation to the component so that it is rotated by changing the rotation angle. 298 .rotate({ angle: this.angle }) 299 .gesture( 300 RotationGesture() 301 .onActionStart((event: GestureEvent|undefined) => { 302 console.info('RotationGesture is onActionStart'); 303 }) 304 // When the rotation gesture takes effect, the rotation angle is obtained through the callback and the component is rotated accordingly. 305 .onActionUpdate((event: GestureEvent|undefined) => { 306 if(event){ 307 this.angle = this.rotateValue + event.angle; 308 } 309 console.info('RotationGesture is onActionEnd'); 310 }) 311 // When the fingers lift from the screen, the component is fixed at the angle where rotation ends. 312 .onActionEnd(() => { 313 this.rotateValue = this.angle; 314 console.info('RotationGesture is onActionEnd'); 315 }) 316 .onActionCancel(() => { 317 console.info('RotationGesture is onActionCancel'); 318 }) 319 ) 320 } 321 .height(200) 322 .width(250) 323 } 324} 325``` 326 327 328 329 330 331## Swipe Gesture 332 333 334```ts 335SwipeGesture(value?:{fingers?:number; direction?:SwipeDirection; speed?:number}) 336``` 337 338 339Triggers a swipe gesture, which can be recognized when the swipe speed is 100 vp/s or higher. This API has three optional parameters: 340 341 342- **fingers**: minimum number of fingers required for gesture recognition. The value ranges from 1 to 10. The default value is **1**. 343 344- **direction**: swipe direction. The enumerated value supports the AND (&) and OR (\|) operations. The default value is **SwipeDirection.All**. 345 346- **speed**: minimum speed of the swipe gesture, in vp/s. The default value is **100**. 347 348 349The following exemplifies how to bind a swipe gesture to the **\<Column>** component to rotate the component: 350 351 352 353```ts 354// xxx.ets 355@Entry 356@Component 357struct Index { 358 @State rotateAngle: number = 0; 359 @State speed: number = 1; 360 361 build() { 362 Column() { 363 Column() { 364 Text("SwipeGesture speed\n" + this.speed) 365 Text("SwipeGesture angle\n" + this.rotateAngle) 366 } 367 .border({ width: 3 }) 368 .width(300) 369 .height(200) 370 .margin(100) 371 // Bind rotation to the <Column> component and change the rotation angle through the swipe speed and angle. 372 .rotate({ angle: this.rotateAngle }) 373 .gesture( 374 // Bind to the component the swipe gesture that can be triggered only when the user swipes in the vertical direction. 375 SwipeGesture({ direction: SwipeDirection.Vertical }) 376 // When the swipe gesture is triggered, the swipe speed and angle are obtained, which can be used to modify the layout parameters. 377 .onAction((event: GestureEvent|undefined) => { 378 if(event){ 379 this.speed = event.speed; 380 this.rotateAngle = event.angle; 381 } 382 }) 383 ) 384 } 385 } 386} 387``` 388 389 390 391 392 393>**NOTE** 394> 395>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**. 396