1# Binding Gesture Events 2 3 4The ArkUI development framework provides tap, drag, swipe, long press, pinch, and rotation gestures through its NDK APIs. By binding different gestures to specified components and setting corresponding callbacks, you can achieve the desired gesture interaction capabilities. 5 6 7The following is a simple example to illustrate how to implement gesture binding. 8 9 101. Create a **Column** node to which gestures will be bound. 11 ``` 12 // Create a Column node. 13 auto column = nodeAPI->createNode(ARKUI_NODE_COLUMN); 14 // Set the background color. 15 ArkUI_NumberValue value[] = {{.u32 = 0xff112233}}; 16 ArkUI_AttributeItem item = {value, 1}; 17 nodeAPI->setAttribute(column, NODE_BACKGROUND_COLOR, &item); 18 // Set the width. 19 ArkUI_NumberValue widthValue[] = {{400}}; 20 ArkUI_AttributeItem width = {widthValue, 1}; 21 nodeAPI->setAttribute(column, NODE_WIDTH, &width); 22 //Set the height. 23 ArkUI_NumberValue heightValue[] = {{400}}; 24 ArkUI_AttributeItem height = {heightValue, 1}; 25 nodeAPI->setAttribute(column, NODE_HEIGHT, &height); 26 ``` 27 282. Create a single-finger long-press gesture with a 1-second activation threshold. 29 ``` 30 // Obtain the set of native gesture APIs. 31 auto gestureApi = reinterpret_cast<ArkUI_NativeGestureAPI_1 *>( 32 OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_GESTURE, "ArkUI_NativeGestureAPI_1")); 33 // Create a long press gesture. 34 auto longPressGesture = gestureApi->createLongPressGesture(1, true, 1000); 35 ``` 36 373. Bind the created gesture to the **Column** node created in step 1. 38 ``` 39 // Set the callback. 40 auto onActionCallBack = [](ArkUI_GestureEvent *event, void *extraParam) { 41 // Callback content 42 }; 43 44 // Set the gesture to the component. 45 gestureApi->setGestureEventTarget(longPressGesture, GESTURE_EVENT_ACTION_ACCEPT | GESTURE_EVENT_ACTION_UPDATE | GESTURE_EVENT_ACTION_END, column, onActionCallBack); 46 47 gestureApi->addGestureToNode(column, longPressGesture, PARALLEL, NORMAL_GESTURE_MASK); 48 ``` 49 50 51## Single Gestures 52 53The following outlines how to create single gestures and the event callbacks they support: 54 55- Tap gesture 56 Triggers a callback on tap, set by the number of taps and fingers required. 57 58 ``` 59 ArkUI_GestureRecognizer* (*createTapGesture)(int32_t countNum, int32_t fingersNum); 60 ``` 61 62- Pan gesture 63 Triggers a callback during dragging, set by the number of fingers, direction, and distance in px. 64 ``` 65 ArkUI_GestureRecognizer* (*createPanGesture)( 66 int32_t fingersNum, ArkUI_GestureDirectionMask directions, double distanceNum); 67 ``` 68 69- Long press gesture 70 Triggers a callback on long press, set by the number of fingers, hold-down time in milliseconds, and repeatability. 71 72 ``` 73 ArkUI_GestureRecognizer* (*createLongPressGesture)(int32_t fingersNum, bool repeatResult, int32_t durationNum); 74 ``` 75 76- Pinch gesture 77 Triggers a callback on pinching, set by the number of fingers (at least 2) and the pinch distance (in px). 78 79 ``` 80 ArkUI_GestureRecognizer* (*createPinchGesture)(int32_t fingersNum, double distanceNum); 81 ``` 82 83- Rotation gesture 84 Triggers a callback on rotation, set by the number of fingers (at least 2) and angle. 85 86 ``` 87 ArkUI_GestureRecognizer* (*createRotationGesture)(int32_t fingersNum, double angleNum); 88 ``` 89 90- Swipe gesture 91 Triggers a callback on swiping, set by the number of fingers (at least 1), direction, and speed in px/s. 92 93 ``` 94 ArkUI_GestureRecognizer* (*createSwipeGesture)( 95 int32_t fingersNum, ArkUI_GestureDirectionMask directions, double speedNum); 96 ``` 97 98 99## Combined Gestures 100 101Combined gestures are created by combining multiple single gestures, and their types are declared with [ArkUI_GroupGestureMode](../reference/apis-arkui/_ark_u_i___native_module.md#arkui_groupgesturemode) in **GroupGesture**. 102 103**ArkUI_GroupGestureMode** supports the following options: **SEQUENTIAL_GROUP** (sequential recognition), **PARALLEL_GROUP** (parallel recognition), **EXCLUSIVE_GROUP** (exclusive recognition). 104 105 106### Sequential Recognition 107 108For combined gestures with sequential recognition, the value of **ArkUI_GroupGestureMode** is **SEQUENTIAL_GROUP**. In this gesture recognition mode, gestures are recognized in the order they were registered until they are all recognized successfully. If any of the registered gestures fails to be recognized, subsequent gestures will also fail. Only the last gesture in a sequential group can respond to the [GESTURE_EVENT_ACTION_END](../reference/apis-arkui/_ark_u_i___native_module.md#arkui_gestureeventactiontype) event. 109 110The following demonstrates how to create a combined gesture that recognizes a long press followed by a swipe in sequence: 111 112``` 113ArkUI_NodeHandle testGestureExample() { 114 auto column = nodeAPI->createNode(ARKUI_NODE_COLUMN); 115 116 // Create a Column node. 117 ArkUI_NumberValue value[] = {{.u32 = 0xff112233}}; 118 ArkUI_AttributeItem item = {value, 1}; 119 nodeAPI->setAttribute(column, NODE_BACKGROUND_COLOR, &item); 120 ArkUI_NumberValue widthValue[] = {{200}}; 121 ArkUI_AttributeItem width = {widthValue, 1}; 122 nodeAPI->setAttribute(column, NODE_WIDTH, &width); 123 ArkUI_NumberValue heightValue[] = {{200}}; 124 ArkUI_AttributeItem height = {heightValue, 1}; 125 nodeAPI->setAttribute(column, NODE_HEIGHT, &height); 126 127 // Check for the gesture API. 128 auto gestureApi = reinterpret_cast<ArkUI_NativeGestureAPI_1 *>( 129 OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_GESTURE, "ArkUI_NativeGestureAPI_1")); 130 if (gestureApi->createGroupGesture) { 131 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", 132 "onPanActionCallBack, createGroupGesture api exist"); 133 } else { 134 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", 135 "onPanActionCallBack, createGroupGesture api not exist"); 136 } 137 auto groupGesture = gestureApi->createGroupGesture(ArkUI_GroupGestureMode::SEQUENTIAL_GROUP); 138 139 // Create a long press gesture. 140 auto longPressGesture = gestureApi->createLongPressGesture(1, true, 500); 141 if (gestureApi->getGestureType) { 142 ArkUI_GestureRecognizerType type = gestureApi->getGestureType(longPressGesture); 143 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", 144 "onPanActionCallBack longPressGesture,ArkUI_GestureRecognizerType%{public}d", type); 145 } 146 // Set a callback for the long press gesture. 147 auto onActionCallBackPanLongPress = [](ArkUI_GestureEvent *event, void *extraParam) { 148 ArkUI_GestureEventActionType actionType = OH_ArkUI_GestureEvent_GetActionType(event); 149 150 float velocity = OH_ArkUI_PanGesture_GetVelocity(event); 151 float velocityX = OH_ArkUI_PanGesture_GetVelocityX(event); 152 float velocityY = OH_ArkUI_PanGesture_GetVelocityY(event); 153 float OffsetX = OH_ArkUI_PanGesture_GetOffsetX(event); 154 float OffsetY = OH_ArkUI_PanGesture_GetOffsetY(event); 155 float scale = OH_ArkUI_PinchGesture_GetScale(event); 156 float CenterX = OH_ArkUI_PinchGesture_GetCenterX(event); 157 float CenterY = OH_ArkUI_PinchGesture_GetCenterY(event); 158 float angle = OH_ArkUI_SwipeGesture_GetAngle(event); 159 float VelocityS = OH_ArkUI_SwipeGesture_GetVelocity(event); 160 float angleR = OH_ArkUI_RotationGesture_GetAngle(event); 161 float repeat = OH_ArkUI_LongPress_GetRepeatCount(event); 162 163 OH_LOG_Print( 164 LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", 165 "onPanActionCallBack,longPressGesturecallback actionType:%{public}d,velocity%{public}f,velocityX" 166 "%{public}f;" 167 "velocityY%{public}f,OffsetX%{public}f,OffsetY%{public}f,scale%{public}fCenterX" 168 "%{public}fCenterY" 169 "%{public}fangle%{public}fVelocityS%{public}fangleR%{public}frepeat%{public}f", 170 actionType, velocity, velocityX, velocityY, OffsetX, OffsetY, scale, CenterX, CenterY, angle, VelocityS, 171 angleR, repeat); 172 }; 173 gestureApi->setGestureEventTarget(longPressGesture, 174 GESTURE_EVENT_ACTION_ACCEPT | GESTURE_EVENT_ACTION_UPDATE | GESTURE_EVENT_ACTION_CANCEL, 175 column, onActionCallBackPanLongPress); 176 177 // Add the long press gesture to the gesture group. 178 if (gestureApi->addChildGesture) { 179 gestureApi->addChildGesture(groupGesture, longPressGesture); 180 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", "onPanActionCallBack, addChildGesture longPressGesture"); 181 } 182 // Create a swipe gesture. 183 auto swipeGesture = gestureApi->createSwipeGesture(1, GESTURE_DIRECTION_ALL, 100); 184 if (gestureApi->getGestureType) { 185 ArkUI_GestureRecognizerType type = gestureApi->getGestureType(swipeGesture); 186 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", 187 "onPanActionCallBack, ArkUI_GestureRecognizerType %{public}d", type); 188 } 189 // Set a callback for the swipe gesture. 190 auto onActionCallBack = [](ArkUI_GestureEvent *event, void *extraParam) { 191 ArkUI_GestureEventActionType actionType = OH_ArkUI_GestureEvent_GetActionType(event); 192 193 float velocity = OH_ArkUI_PanGesture_GetVelocity(event); 194 float velocityX = OH_ArkUI_PanGesture_GetVelocityX(event); 195 float velocityY = OH_ArkUI_PanGesture_GetVelocityY(event); 196 float OffsetX = OH_ArkUI_PanGesture_GetOffsetX(event); 197 float OffsetY = OH_ArkUI_PanGesture_GetOffsetY(event); 198 float scale = OH_ArkUI_PinchGesture_GetScale(event); 199 float CenterX = OH_ArkUI_PinchGesture_GetCenterX(event); 200 float CenterY = OH_ArkUI_PinchGesture_GetCenterY(event); 201 float angle = OH_ArkUI_SwipeGesture_GetAngle(event); 202 float VelocityS = OH_ArkUI_SwipeGesture_GetVelocity(event); 203 float angleR = OH_ArkUI_RotationGesture_GetAngle(event); 204 float repeat = OH_ArkUI_LongPress_GetRepeatCount(event); 205 206 207 // Print logs. 208 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", 209 "onPanActionCallBack, swipeGesture callback actionType: %{public}d, velocity " 210 "%{public}f,velocityX " 211 "%{public}f; " 212 "velocityY %{public}f, OffsetX %{public}f, OffsetY %{public}f, scale %{public}fCenterX " 213 "%{public}f CenterY" 214 " %{public}f angle %{public}f VelocityS %{public}f angleR %{public}f repeat %{public}f", 215 actionType, velocity, velocityX, velocityY, OffsetX, OffsetY, scale, CenterX, CenterY, angle, 216 VelocityS, angleR, repeat); 217 218 ArkUI_NumberValue value[] = {{.f32 = 0}, {.f32 = 0}, {.f32 = 0}, {.f32 = angleR}, {.f32 = 0}}; 219 ArkUI_AttributeItem item = {value, 5}; 220 auto column = reinterpret_cast<ArkUI_NodeHandle>(extraParam); 221 nodeAPI->setAttribute(column, NODE_ROTATE, &item); 222 }; 223 224 gestureApi->setGestureEventTarget( 225 swipeGesture, GESTURE_EVENT_ACTION_ACCEPT | GESTURE_EVENT_ACTION_UPDATE | GESTURE_EVENT_ACTION_END, column, 226 onActionCallBack); 227 228 // Add the swipe press gesture to the gesture group. 229 if (gestureApi->addChildGesture) { 230 gestureApi->addChildGesture(groupGesture, swipeGesture); 231 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", 232 "onPanActionCallBack, addChildGesture swipeGesture"); 233 } 234 // Set the gesture group to the component. 235 gestureApi->addGestureToNode(column, groupGesture, PRIORITY, NORMAL_GESTURE_MASK); 236 return column; 237} 238``` 239 240 241### Parallel Recognition 242 243For combined gestures with parallel recognition, the value of **ArkUI_GroupGestureMode** is **PARALLEL_GROUP**. In this gesture recognition mode, gestures registered in the combined gestures will be recognized at the same time until they are all recognized successfully. The gestures are recognized in parallel without affecting each other. 244 245The following demonstrates how to create a combined gesture that recognizes long press and swipe gestures in parallel: 246 247``` 248ArkUI_NodeHandle testGestureExample() { 249 auto column = nodeAPI->createNode(ARKUI_NODE_COLUMN); 250 251 // Create a Column node. 252 ArkUI_NumberValue value[] = {{.u32 = 0xff112233}}; 253 ArkUI_AttributeItem item = {value, 1}; 254 nodeAPI->setAttribute(column, NODE_BACKGROUND_COLOR, &item); 255 ArkUI_NumberValue widthValue[] = {{200}}; 256 ArkUI_AttributeItem width = {widthValue, 1}; 257 nodeAPI->setAttribute(column, NODE_WIDTH, &width); 258 ArkUI_NumberValue heightValue[] = {{200}}; 259 ArkUI_AttributeItem height = {heightValue, 1}; 260 nodeAPI->setAttribute(column, NODE_HEIGHT, &height); 261 262 // Check for the gesture API. 263 auto gestureApi = reinterpret_cast<ArkUI_NativeGestureAPI_1 *>( 264 OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_GESTURE, "ArkUI_NativeGestureAPI_1")); 265 if (gestureApi->createGroupGesture) { 266 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", 267 "onPanActionCallBack, createGroupGesture api exist"); 268 } else { 269 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", 270 "onPanActionCallBack, createGroupGesture api not exist"); 271 } 272 273 // Create a gesture group. 274 auto groupGesture = gestureApi->createGroupGesture(ArkUI_GroupGestureMode::PARALLEL_GROUP); 275 276 // Create a long press gesture. 277 auto longPressGesture = gestureApi->createLongPressGesture(1, true, 500); 278 if (gestureApi->getGestureType) { 279 ArkUI_GestureRecognizerType type = gestureApi->getGestureType(longPressGesture); 280 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", 281 "onPanActionCallBack,ArkUI_GestureRecognizerType%{public}d", type); 282 } 283 // Set a callback for the long press gesture. 284 auto onActionCallBackPanLongPress = [](ArkUI_GestureEvent *event, void *extraParam) { 285 ArkUI_GestureEventActionType actionType = OH_ArkUI_GestureEvent_GetActionType(event); 286 287 float velocity = OH_ArkUI_PanGesture_GetVelocity(event); 288 float velocityX = OH_ArkUI_PanGesture_GetVelocityX(event); 289 float velocityY = OH_ArkUI_PanGesture_GetVelocityY(event); 290 float OffsetX = OH_ArkUI_PanGesture_GetOffsetX(event); 291 float OffsetY = OH_ArkUI_PanGesture_GetOffsetY(event); 292 float scale = OH_ArkUI_PinchGesture_GetScale(event); 293 float CenterX = OH_ArkUI_PinchGesture_GetCenterX(event); 294 float CenterY = OH_ArkUI_PinchGesture_GetCenterY(event); 295 float angle = OH_ArkUI_SwipeGesture_GetAngle(event); 296 float VelocityS = OH_ArkUI_SwipeGesture_GetVelocity(event); 297 float angleR = OH_ArkUI_RotationGesture_GetAngle(event); 298 float repeat = OH_ArkUI_LongPress_GetRepeatCount(event); 299 300 OH_LOG_Print( 301 LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", 302 "onPanActionCallBack,longPressGesturecallback actionType:%{public}d,velocity%{public}f,velocityX" 303 "%{public}f;" 304 "velocityY%{public}f,OffsetX%{public}f,OffsetY%{public}f,scale%{public}fCenterX" 305 "%{public}fCenterY" 306 "%{public}fangle%{public}fVelocityS%{public}fangleR%{public}frepeat%{public}f", 307 actionType, velocity, velocityX, velocityY, OffsetX, OffsetY, scale, CenterX, CenterY, angle, VelocityS, 308 angleR, repeat); 309 }; 310 gestureApi->setGestureEventTarget(longPressGesture, 311 GESTURE_EVENT_ACTION_ACCEPT | GESTURE_EVENT_ACTION_UPDATE | 312 GESTURE_EVENT_ACTION_CANCEL, 313 column, onActionCallBackPanLongPress); 314 315 // Add the long press gesture to the gesture group. 316 if (gestureApi->addChildGesture) { 317 gestureApi->addChildGesture(groupGesture, longPressGesture); 318 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", "onPanActionCallBack, addChildGesture longPressGesture"); 319 } 320 // Create a swipe gesture. 321 auto swipeGesture = gestureApi->createSwipeGesture(1, GESTURE_DIRECTION_ALL, 100); 322 if (gestureApi->getGestureType) { 323 ArkUI_GestureRecognizerType type = gestureApi->getGestureType(swipeGesture); 324 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", 325 "onPanActionCallBack, ArkUI_GestureRecognizerType %{public}d", type); 326 } 327 // Set a callback for the swipe gesture. 328 auto onActionCallBack = [](ArkUI_GestureEvent *event, void *extraParam) { 329 ArkUI_GestureEventActionType actionType = OH_ArkUI_GestureEvent_GetActionType(event); 330 331 float velocity = OH_ArkUI_PanGesture_GetVelocity(event); 332 float velocityX = OH_ArkUI_PanGesture_GetVelocityX(event); 333 float velocityY = OH_ArkUI_PanGesture_GetVelocityY(event); 334 float OffsetX = OH_ArkUI_PanGesture_GetOffsetX(event); 335 float OffsetY = OH_ArkUI_PanGesture_GetOffsetY(event); 336 float scale = OH_ArkUI_PinchGesture_GetScale(event); 337 float CenterX = OH_ArkUI_PinchGesture_GetCenterX(event); 338 float CenterY = OH_ArkUI_PinchGesture_GetCenterY(event); 339 float angle = OH_ArkUI_SwipeGesture_GetAngle(event); 340 float VelocityS = OH_ArkUI_SwipeGesture_GetVelocity(event); 341 float angleR = OH_ArkUI_RotationGesture_GetAngle(event); 342 float repeat = OH_ArkUI_LongPress_GetRepeatCount(event); 343 344 345 // Print logs. 346 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", 347 "onPanActionCallBack, swipeGesture callback actionType: %{public}d, velocity " 348 "%{public}f,velocityX " 349 "%{public}f; " 350 "velocityY %{public}f, OffsetX %{public}f, OffsetY %{public}f, scale %{public}fCenterX " 351 "%{public}f CenterY" 352 " %{public}f angle %{public}f VelocityS %{public}f angleR %{public}f repeat %{public}f", 353 actionType, velocity, velocityX, velocityY, OffsetX, OffsetY, scale, CenterX, CenterY, angle, 354 VelocityS, angleR, repeat); 355 356 ArkUI_NumberValue value[] = {{.f32 = 0}, {.f32 = 0}, {.f32 = 0}, {.f32 = angleR}, {.f32 = 0}}; 357 ArkUI_AttributeItem item = {value, 5}; 358 auto column = reinterpret_cast<ArkUI_NodeHandle>(extraParam); 359 nodeAPI->setAttribute(column, NODE_ROTATE, &item); 360 }; 361 362 gestureApi->setGestureEventTarget( 363 swipeGesture, GESTURE_EVENT_ACTION_ACCEPT | GESTURE_EVENT_ACTION_UPDATE | GESTURE_EVENT_ACTION_END, column, 364 onActionCallBack); 365 366 // Add the swipe press gesture to the gesture group. 367 if (gestureApi->addChildGesture) { 368 gestureApi->addChildGesture(groupGesture, swipeGesture); 369 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", 370 "onPanActionCallBack, addChildGesture swipeGesture"); 371 } 372 // Set the gesture group to the component. 373 gestureApi->addGestureToNode(column, groupGesture, PRIORITY, NORMAL_GESTURE_MASK); 374 return column; 375} 376``` 377 378 379### Exclusive Recognition 380 381For combined gestures with exclusive recognition, the value of **ArkUI_GroupGestureMode** is **EXCLUSIVE_GROUP**. In this gesture recognition mode, all registered gestures are recognized at once. Once any of the gestures is recognized successfully, the gesture recognition ends, and recognition for all other gestures fails. 382 383The following example illustrates the exclusive recognition of pan and pinch gestures: 384 385``` 386ArkUI_NodeHandle testGestureExample() { 387 auto column = nodeAPI->createNode(ARKUI_NODE_COLUMN); 388 auto button = nodeAPI->createNode(ARKUI_NODE_BUTTON); 389 390 // Create a Column node. 391 ArkUI_NumberValue value[] = {{.u32 = 0xff112233}}; 392 ArkUI_AttributeItem item = {value, 1}; 393 nodeAPI->setAttribute(column, NODE_BACKGROUND_COLOR, &item); 394 ArkUI_NumberValue widthValue[] = {{200}}; 395 ArkUI_AttributeItem width = {widthValue, 1}; 396 nodeAPI->setAttribute(column, NODE_WIDTH, &width); 397 ArkUI_NumberValue heightValue[] = {{200}}; 398 ArkUI_AttributeItem height = {heightValue, 1}; 399 nodeAPI->setAttribute(column, NODE_HEIGHT, &height); 400 401 // Check for the gesture API. 402 auto gestureApi = reinterpret_cast<ArkUI_NativeGestureAPI_1 *>( 403 OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_GESTURE, "ArkUI_NativeGestureAPI_1")); 404 if (gestureApi->createGroupGesture) { 405 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", 406 "onPanActionCallBack, createGroupGesture api exist"); 407 } else { 408 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", 409 "onPanActionCallBack, createGroupGesture api not exist"); 410 } 411 auto groupGesture = gestureApi->createGroupGesture(ArkUI_GroupGestureMode::EXCLUSIVE_GROUP); 412 413 // Create a pan gesture. 414 auto panGesture = gestureApi->createPanGesture(1, GESTURE_DIRECTION_VERTICAL, 5); 415 if (gestureApi->getGestureType) { 416 ArkUI_GestureRecognizerType type = gestureApi->getGestureType(panGesture); 417 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", 418 "onPanActionCallBack panGesture, ArkUI_GestureRecognizerType %{public}d", type); 419 } 420 // Set a callback for the pan gesture. 421 auto onActionCallBackPan = [](ArkUI_GestureEvent *event, void *extraParam) { 422 ArkUI_GestureEventActionType actionType = OH_ArkUI_GestureEvent_GetActionType(event); 423 424 float velocity = OH_ArkUI_PanGesture_GetVelocity(event); 425 float velocityX = OH_ArkUI_PanGesture_GetVelocityX(event); 426 float velocityY = OH_ArkUI_PanGesture_GetVelocityY(event); 427 float OffsetX = OH_ArkUI_PanGesture_GetOffsetX(event); 428 float OffsetY = OH_ArkUI_PanGesture_GetOffsetY(event); 429 float scale = OH_ArkUI_PinchGesture_GetScale(event); 430 float CenterX = OH_ArkUI_PinchGesture_GetCenterX(event); 431 float CenterY = OH_ArkUI_PinchGesture_GetCenterY(event); 432 float angle = OH_ArkUI_SwipeGesture_GetAngle(event); 433 float VelocityS = OH_ArkUI_SwipeGesture_GetVelocity(event); 434 float angleR = OH_ArkUI_RotationGesture_GetAngle(event); 435 float repeat = OH_ArkUI_LongPress_GetRepeatCount(event); 436 437 // Print logs. 438 OH_LOG_Print( 439 LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", 440 "onPanActionCallBack, panGesture callback actionType: %{public}d, velocity %{public}f,velocityX " 441 "%{public}f; " 442 "velocityY %{public}f, OffsetX %{public}f, OffsetY %{public}f, scale %{public}fCenterX " 443 "%{public}f CenterY" 444 " %{public}f angle %{public}f VelocityS %{public}f angleR %{public}f repeat %{public}f", 445 actionType, velocity, velocityX, velocityY, OffsetX, OffsetY, scale, CenterX, CenterY, angle, VelocityS, 446 angleR, repeat); 447 }; 448 gestureApi->setGestureEventTarget(panGesture, 449 GESTURE_EVENT_ACTION_ACCEPT | GESTURE_EVENT_ACTION_UPDATE | 450 GESTURE_EVENT_ACTION_END | GESTURE_EVENT_ACTION_CANCEL, 451 column, onActionCallBackPan); 452 // Add the pan press gesture to the gesture group. 453 if (gestureApi->addChildGesture) { 454 gestureApi->addChildGesture(groupGesture, panGesture); 455 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", "onPanActionCallBack, addChildGesture panGesture"); 456 } 457 // Create a pinch gesture. 458 auto pinchGesture = gestureApi->createPinchGesture(0, 0); 459 if (gestureApi->getGestureType) { 460 ArkUI_GestureRecognizerType type = gestureApi->getGestureType(pinchGesture); 461 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", 462 "onPanActionCallBack pinchGesture, ArkUI_GestureRecognizerType %{public}d", type); 463 } 464 // Set a callback for the pinch gesture. 465 auto onActionCallBack = [](ArkUI_GestureEvent *event, void *extraParam) { 466 ArkUI_GestureEventActionType actionType = OH_ArkUI_GestureEvent_GetActionType(event); 467 468 float velocity = OH_ArkUI_PanGesture_GetVelocity(event); 469 float velocityX = OH_ArkUI_PanGesture_GetVelocityX(event); 470 float velocityY = OH_ArkUI_PanGesture_GetVelocityY(event); 471 float OffsetX = OH_ArkUI_PanGesture_GetOffsetX(event); 472 float OffsetY = OH_ArkUI_PanGesture_GetOffsetY(event); 473 float scale = OH_ArkUI_PinchGesture_GetScale(event); 474 float CenterX = OH_ArkUI_PinchGesture_GetCenterX(event); 475 float CenterY = OH_ArkUI_PinchGesture_GetCenterY(event); 476 float angle = OH_ArkUI_SwipeGesture_GetAngle(event); 477 float VelocityS = OH_ArkUI_SwipeGesture_GetVelocity(event); 478 float angleR = OH_ArkUI_RotationGesture_GetAngle(event); 479 float repeat = OH_ArkUI_LongPress_GetRepeatCount(event); 480 481 482 OH_LOG_Print( 483 LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", 484 "onPanActionCallBack, pinchGesture callback actionType: %{public}d, velocity %{public}f,velocityX " 485 "%{public}f; " 486 "velocityY %{public}f, OffsetX %{public}f, OffsetY %{public}f, scale %{public}fCenterX " 487 "%{public}f CenterY" 488 " %{public}f angle %{public}f VelocityS %{public}f angleR %{public}f repeat %{public}f", 489 actionType, velocity, velocityX, velocityY, OffsetX, OffsetY, scale, CenterX, CenterY, angle, VelocityS, 490 angleR, repeat); 491 492 493 ArkUI_NumberValue value[] = {{.f32 = scale}, {.f32 = scale}}; 494 ArkUI_AttributeItem item = {value, 2}; 495 auto column = reinterpret_cast<ArkUI_NodeHandle>(extraParam); 496 nodeAPI->setAttribute(column, NODE_SCALE, &item); 497 }; 498 gestureApi->setGestureEventTarget(pinchGesture, 499 GESTURE_EVENT_ACTION_ACCEPT | GESTURE_EVENT_ACTION_UPDATE | 500 GESTURE_EVENT_ACTION_END | GESTURE_EVENT_ACTION_CANCEL, 501 column, onActionCallBack); 502 // Add the pinch press gesture to the gesture group. 503 if (gestureApi->addChildGesture) { 504 gestureApi->addChildGesture(groupGesture, pinchGesture); 505 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", "onPanActionCallBack, addChildGesture pinchGesture"); 506 } 507 // Set the gesture group to the component. 508 gestureApi->addGestureToNode(column, groupGesture, PRIORITY, NORMAL_GESTURE_MASK); 509 return column; 510} 511``` 512 513 514### Custom Gesture Judgment 515 516With support for custom gesture judgment, you can determine whether to continue executing a gesture based on callback content. 517 518To implement custom gesture judgment, adjust the example of binding gesture events as follows: 519 520 5211. Create a custom gesture judgment callback. 522 ``` 523 auto onInterruptCallback = [](ArkUI_GestureInterruptInfo *info) -> ArkUI_GestureInterruptResult { 524 // Check whether the gesture is a system gesture. 525 auto systag = OH_ArkUI_GestureInterruptInfo_GetSystemFlag(info); 526 // Obtain the gesture recognizer for gesture interception. 527 auto recognizer = OH_ArkUI_GestureInterruptInfo_GetRecognizer(info); 528 // Obtain the system gesture type. 529 auto systemRecognizerType = OH_ArkUI_GestureInterruptInfo_GetSystemRecognizerType(info); 530 // Obtain the gesture event. 531 auto gestureEvent = OH_ArkUI_GestureInterruptInfo_GetGestureEvent(info); 532 auto inputevent = OH_ArkUI_GestureEvent_GetRawInputEvent(gestureEvent); 533 534 if (systag) { 535 // If the gesture is a system gesture, do not intercept it. 536 return GESTURE_INTERRUPT_RESULT_CONTINUE; 537 } else { 538 // If the gesture is not a system gesture, intercept and reject it. 539 return GESTURE_INTERRUPT_RESULT_REJECT; 540 } 541 }; 542 ``` 543 5442. Bind the custom gesture interrupter to the node. 545 ``` 546 gestureApi->setGestureInterrupterToNode(column, onInterruptCallback); 547 ``` 548 549 550After the aforementioned modifications, the originally effective long press gesture is intercepted. Consequently, long pressing the **Column** node will no longer trigger the long press gesture callback. 551