1# 绑定手势事件 2 3 4ArkUI开发框架在NDK接口主要提供点击手势、拖动手势、滑动手势、长按手势、捏合手势和旋转手势,通过给指定的组件绑定不同的手势并设置相应的回调,实现期望的手势交互能力。 5 6 7下面通过一个简单的示例来介绍如何实现手势绑定。 8 9 101. 创建一个Column节点,用于绑定手势。 11 ``` 12 // 创建Column节点 13 auto column = nodeAPI->createNode(ARKUI_NODE_COLUMN); 14 // 设置背景色 15 ArkUI_NumberValue value[] = {{.u32 = 0xff112233}}; 16 ArkUI_AttributeItem item = {value, 1}; 17 nodeAPI->setAttribute(column, NODE_BACKGROUND_COLOR, &item); 18 // 设置宽度 19 ArkUI_NumberValue widthValue[] = {{400}}; 20 ArkUI_AttributeItem width = {widthValue, 1}; 21 nodeAPI->setAttribute(column, NODE_WIDTH, &width); 22 // 设置高度 23 ArkUI_NumberValue heightValue[] = {{400}}; 24 ArkUI_AttributeItem height = {heightValue, 1}; 25 nodeAPI->setAttribute(column, NODE_HEIGHT, &height); 26 ``` 27 282. 创建一个单指长按1秒并持续响应的长按手势。 29 ``` 30 // 获取手势Native接口集合 31 auto gestureApi = reinterpret_cast<ArkUI_NativeGestureAPI_1 *>( 32 OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_GESTURE, "ArkUI_NativeGestureAPI_1")); 33 // 创建长按手势 34 auto longPressGesture = gestureApi->createLongPressGesture(1, true, 1000); 35 ``` 36 373. 将创建的手势和步骤一中创建的Column节点绑定。 38 ``` 39 // 设置回调 40 auto onActionCallBack = [](ArkUI_GestureEvent *event, void *extraParam) { 41 // 回调内容 42 }; 43 44 // 将手势设置到组件上 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## 单一手势 52 53通过上文的示例已经了解了如果将手势绑定在节点上,接下来将分别介绍不同手势的创建方法,并分别支持哪些事件回调。 54 55- 点击手势 56 通过给组件绑定点击手势可在组件被点击时触发此回调,可指定触发回调需要的点击次数和手指个数。 57 58 ``` 59 ArkUI_GestureRecognizer* (*createTapGesture)(int32_t countNum, int32_t fingersNum); 60 ``` 61 62- 拖动手势 63 通过给组件绑定拖动手势可在用户拖动组件时触发回调,可指定触发回调需要的手指个数、拖动方向、拖动距离。单位为px。 64 ``` 65 ArkUI_GestureRecognizer* (*createPanGesture)( 66 int32_t fingersNum, ArkUI_GestureDirectionMask directions, double distanceNum); 67 ``` 68 69- 长按手势 70 通过给组件绑定长按手势可在用户长按组件时触发回调,可指定触发回调需要的手指个数、长按时间(单位毫秒)、是否连续触发。 71 72 ``` 73 ArkUI_GestureRecognizer* (*createLongPressGesture)(int32_t fingersNum, bool repeatResult, int32_t durationNum); 74 ``` 75 76- 捏合手势 77 通过给组件绑定捏合手势可在用户捏合组件时触发回调,可指定触发回调需要的手指个数(最小为2)、捏合距离(单位px)。 78 79 ``` 80 ArkUI_GestureRecognizer* (*createPinchGesture)(int32_t fingersNum, double distanceNum); 81 ``` 82 83- 旋转手势 84 通过给组件绑定旋转手势可在用户旋转组件时触发回调,可指定触发回调需要的手指个数(最小为2)、旋转角度。 85 86 ``` 87 ArkUI_GestureRecognizer* (*createRotationGesture)(int32_t fingersNum, double angleNum); 88 ``` 89 90- 滑动手势 91 通过给组件绑定滑动手势可在用户滑动组件时触发回调,可指定触发回调需要的手指个数(最小为1)、滑动方向、滑动速度(单位px/s)。 92 93 ``` 94 ArkUI_GestureRecognizer* (*createSwipeGesture)( 95 int32_t fingersNum, ArkUI_GestureDirectionMask directions, double speedNum); 96 ``` 97 98 99## 组合手势 100 101组合手势由多种单一手势组合而成,通过在GroupGesture中使用不同的[ArkUI_GroupGestureMode](../reference/apis-arkui/_ark_u_i___native_module.md#arkui_groupgesturemode)来声明该组合手势的类型,支持顺序识别、并行识别、互斥识别三种类型。 102 103ArkUI_GroupGestureMode枚举类,用于声明该组合手势的类型。顺序识别SEQUENTIAL_GROUP,并行识别PARALLEL_GROUP,互斥识别EXCLUSIVE_GROUP。 104 105 106### 顺序识别 107 108顺序识别组合手势对应的ArkUI_GroupGestureMode为SEQUENTIAL_GROUP。顺序识别组合手势将按照手势的注册顺序识别手势,直到所有的手势识别成功。当顺序识别组合手势中有一个手势识别失败时,后续手势识别均失败。顺序识别手势组仅有最后一个手势可以响应[GESTURE_EVENT_ACTION_END](../reference/apis-arkui/_ark_u_i___native_module.md#arkui_gestureeventactiontype)。 109 110以顺序识别长按和滑动手势为例: 111 112``` 113ArkUI_NodeHandle testGestureExample() { 114 auto column = nodeAPI->createNode(ARKUI_NODE_COLUMN); 115 116 // 创建手势并设置回调 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 // 判断是否有手势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 // 创建长按手势 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 // 给长按手势定回调 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 // 将长按手势添加到手势组 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 // 创建滑动手势 swipe 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 // 给滑动手势绑定回调 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 // 通过日志查看 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 // 将滑动手势添加到手势组 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 // 将手势组设置到组件上 235 gestureApi->addGestureToNode(column, groupGesture, PRIORITY, NORMAL_GESTURE_MASK); 236 return column; 237} 238``` 239 240 241### 并行识别 242 243并行识别组合手势对应的ArkUI_GroupGestureMode为PARALLEL_GROUP。并行识别组合手势中注册的手势将同时进行识别,直到所有手势识别结束。并行识别手势组合中的手势进行识别时互不影响。 244 245以并行识别长按和滑动手势为例: 246 247``` 248ArkUI_NodeHandle testGestureExample() { 249 auto column = nodeAPI->createNode(ARKUI_NODE_COLUMN); 250 251 // 创建手势并设置回调 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 // 判断是否有手势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 // 创建手势组 274 auto groupGesture = gestureApi->createGroupGesture(ArkUI_GroupGestureMode::PARALLEL_GROUP); 275 276 // 创建长按手势 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 // 给长按手势定回调 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 // 将长按手势添加到手势组 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 // 创建滑动手势 swipe 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 // 给滑动手势绑定回调 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 // 通过日志查看 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 // 将滑动手势添加到手势组 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 // 将手势组设置到组件上 373 gestureApi->addGestureToNode(column, groupGesture, PRIORITY, NORMAL_GESTURE_MASK); 374 return column; 375} 376``` 377 378 379### 互斥识别 380 381互斥识别组合手势对应的ArkUI_GroupGestureMode为EXCLUSIVE_GROUP。互斥识别组合手势中注册的手势将同时进行识别,若有一个手势识别成功,则结束手势识别,其他所有手势识别失败。 382 383以互斥识别平移手势和捏合手势为例: 384 385``` 386ArkUI_NodeHandle testGestureExample() { 387 auto column = nodeAPI->createNode(ARKUI_NODE_COLUMN); 388 auto button = nodeAPI->createNode(ARKUI_NODE_BUTTON); 389 390 // 创建手势并设置回调 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 // 判断是否有手势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 // 创建拖动手势 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 // 给拖动手势绑定回调 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 // 通过日志查看 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 // 将拖动手势添加到手势组 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 // 创建捏合手势 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 // 给捏合手势绑定回调 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 // 将捏合手势添加到手势组 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 // 将手势组设置到组件上 508 gestureApi->addGestureToNode(column, groupGesture, PRIORITY, NORMAL_GESTURE_MASK); 509 return column; 510} 511``` 512 513 514### 自定义手势判定 515 516支持自定义手势判定,当组件触发手势时,可根据回调内容判定当前响应的手势是否继续执行。 517 518在上文绑定手势事件的示例中按照如下方式进行调整即可实现自定义手势判定。 519 520 5211. 创建自定义手势判定回调。 522 ``` 523 auto onInterruptCallback = [](ArkUI_GestureInterruptInfo *info) -> ArkUI_GestureInterruptResult { 524 // 获取是否系统手势 525 auto systag = OH_ArkUI_GestureInterruptInfo_GetSystemFlag(info); 526 // 获取拦截的手势指针 527 auto recognizer = OH_ArkUI_GestureInterruptInfo_GetRecognizer(info); 528 // 获取系统手势类型 529 auto systemRecognizerType = OH_ArkUI_GestureInterruptInfo_GetSystemRecognizerType(info); 530 // 获取手势事件 531 auto gestureEvent = OH_ArkUI_GestureInterruptInfo_GetGestureEvent(info); 532 auto inputevent = OH_ArkUI_GestureEvent_GetRawInputEvent(gestureEvent); 533 534 if (systag) { 535 // 如果是系统手势则不拦截 536 return GESTURE_INTERRUPT_RESULT_CONTINUE; 537 } else { 538 // 不是系统手势则拒绝 539 return GESTURE_INTERRUPT_RESULT_REJECT; 540 } 541 }; 542 ``` 543 5442. 绑定手势判定和节点。 545 ``` 546 gestureApi->setGestureInterrupterToNode(column, onInterruptCallback); 547 ``` 548 549 550经过上述修改,将原本可以生效的长按手势做了拦截,即,此时再对Column节点长按将不会触发长按的手势回调。 551