鸿蒙手势的拦截与处理

0 阅读1分钟
1.  @Entry
1.  @Component
1.  export struct OnClickGesture {
1.  private judgeCount: number = 0;
1.
1.  increaseJudgeGuard(): void {
1.  this.judgeCount++;
1.  }
1.
1.  build() {
1.  NavDestination() {
1.  Column() {
1.  Column() {
1.  Column()
1.  .width('60%')
1.  .height('50%')
1.  .backgroundColor(Color.Grey)
1.  .onClick(() => { // 1. 子组件上注册了点击事件,正常情况下点击在子组件上时,优先得到响应
1.  console.info('Clicked on child');
1.  this.increaseJudgeGuard();
1.  })
1.  .onGestureJudgeBegin((gestureInfo: GestureInfo, event: BaseGestureEvent) => {
1.  // 3. 当数字增长为5的倍数时禁用子组件上的点击手势,这样父组件上的点击可以得到响应
1.  if (this.judgeCount % 5 == 0 && gestureInfo.type == GestureControl.GestureType.CLICK) {
1.  return GestureJudgeResult.REJECT;
1.  } else {
1.  return GestureJudgeResult.CONTINUE;
1.  }
1.  })
1.  }
1.  .width('80%')
1.  .height('80%')
1.  .justifyContent(FlexAlign.Center)
1.  .backgroundColor(Color.Green)
1.  .gesture(
1.  TapGesture() // 2. 父组件上注册了点击手势,正常情况下点击在子组件区域时,父组件上的手势优先级低于子组件
1.  .onAction(() => {
1.  console.info('Clicked on parent');
1.  this.increaseJudgeGuard();
1.  }))
1.  }
1.  .height('100%')
1.  .width('100%')
1.  .justifyContent(FlexAlign.Center)
1.  }
1.  .backgroundColor('#f1f2f3')
1.  // 请将$r('app.string.singlegesture_Index_Click_title')替换为实际资源文件,在本示例中该资源文件的value值为"点击事件"
1.  .title($r('app.string.singlegesture_Index_Click_title'))
1.  }
1.  }