HarmonyOS 鸿蒙Next中C API有手势拦截有示例吗?

HarmonyOS 鸿蒙Next中C API有手势拦截有示例吗? C API有手势拦截有示例吗?

3 回复

您好,有的,详见https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-gesture-blocking-enhancement

从API version 20开始,该示例通过配置onTouchTestDone指定手势识别器不参与后续手势处理,触发回调时,调用preventBegin阻止手势识别器参与后续处理。

// xxx.ets
@Entry
@Component
struct TouchTestDoneExample {
  @State tagList: string[] = ['Null', 'Tap1', 'Tap2', 'Tap3', 'Tap4'];
  @State tagId: number = 0;
  @State textValue: string = '';

  // 多层嵌套场景,为每一层的组件绑定一个Tap手势
  build() {
    Column() {
      Column() {
        Text('Tap1')
          .margin(20)
        Column() {
          Text('Tap2')
            .margin(20)
          Column() {
            Text('Tap3')
              .margin(20)
            Column() {
              Text('Tap4')
                .margin(20)
            }
            .backgroundColor('#D5D5D5')
            .width('80%')
            .height('80%')
            .gesture(TapGesture().tag('Tap4').onAction(() => {
              this.textValue = 'Tap4';
            }))
          }
          .backgroundColor('#F7F7F7')
          .width('80%')
          .height('80%')
          .gesture(TapGesture().tag('Tap3').onAction(() => {
            this.textValue = 'Tap3';
          }))
        }
        .backgroundColor('#707070')
        .width('80%')
        .height('80%')
        .gesture(TapGesture().tag('Tap2').onAction(() => {
          this.textValue = 'Tap2';
        }))
      }
      .backgroundColor('#D5D5D5')
      .width('80%')
      .height('80%')
      .gesture(TapGesture().tag('Tap1').onAction(() => {
        this.textValue = 'Tap1';
      }))
      // 绑定onTouchTestDone,通过调用手势识别器的preventBegin()方法来自定义手势识别器是否参与后续手势处理
      .onTouchTestDone((event, recognizers) => {
        console.info('event is ' + JSON.stringify(event));
        for (let i = 0; i < recognizers.length; i++) {
          let recognizer = recognizers[i];
          console.info('type is ' + JSON.stringify(recognizer.getType()))
          // 根据tag的值屏蔽不同的手势识别器
          if (recognizer.getTag() == this.tagList[this.tagId]) {
            recognizer.preventBegin();
          }
        }
      })

      Text('Current Gesture: ' + this.textValue)
        .margin(5)

      Button('Click to change preventGesture')
        .margin(5)
        .onClick(() => {
          this.tagId++;
          this.tagId %= 5;
        })
      Text('Current prevent gesture tag: ' + this.tagList[this.tagId])
        .margin(5)

    }
    .width('100%')
    .height('100%')

    // 示例gif中,点击Tap2和Tap1的重合区域,不调用preventBegin时,触发的为Tap2手势;调用preventBegin阻止Tap2时,触发的为Tap1手势
  }
}

更多关于HarmonyOS 鸿蒙Next中C API有手势拦截有示例吗?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


鸿蒙Next中C API手势拦截可通过OH_Gesture_Intercept接口实现。示例代码片段如下:

OH_Gesture *gesture = OH_Gesture_Create();
OH_Gesture_Intercept(gesture, GESTURE_TYPE_PINCH, intercept_callback);

OH_Bool intercept_callback(OH_Gesture *gesture, OH_Gesture_Event *event) {
    // 拦截逻辑判断
    return OH_TRUE; // 返回OH_TRUE拦截手势
}

需在native_module.json中声明手势权限,使用OH_Gesture_Intercept注册拦截回调函数,通过返回值控制手势传递链。

在HarmonyOS Next中,C API支持通过OH_NativeGesture_Intercept接口实现手势拦截。以下是基础示例:

#include <native_gesture.h>

// 创建手势拦截器示例
OH_NativeGesture* gesture = OH_NativeGesture_Create();

// 设置拦截条件(如拦截所有滑动手势)
OH_NativeGesture_SetInterceptConfig(gesture, GESTURE_TYPE_SWIPE);

// 注册拦截回调
OH_NativeGesture_RegisterInterceptCallback(gesture, [](OH_NativeGestureEvent* event) {
    // 在此处理拦截逻辑
    // 返回true表示拦截手势,false表示放行
    return true;
});

// 绑定到指定窗口
OH_NativeGesture_AttachToWindow(gesture, window_handle);

关键点:

  1. 使用OH_NativeGesture_Create()创建手势实例
  2. 通过SetInterceptConfig配置要拦截的手势类型
  3. 在回调函数中决定是否拦截
  4. 需调用AttachToWindow关联目标窗口

支持拦截的手势类型包括:点击(TAP)、长按(LONG_PRESS)、滑动(SWIPE)、捏合(PINCH)等。具体API定义参考NDK中的native_gesture.h头文件。

回到顶部