当前应用通过Swiper组件垂直滚动展示公告通知,点击子组件的内容会有弹窗提示,左右滑动时也会触发Text组件的点击事件,如何保证在左右滑动时不触发子组件的点击事件?
问题代码示例参考如下:
import { PromptAction } from '@kit.ArkUI'; @Entry @Component struct Index { promptAction: PromptAction = this.getUIContext().getPromptAction(); build() { Column() { Swiper() { Text('公告') .width('80%') .fontSize(20) .textAlign(TextAlign.Center) .onClick(() => { this.promptAction.showToast({ message: '通知详情', alignment: Alignment.Top }); }) } .height('20%') .vertical(true) } .width('100%') .height('100%') } }
问题效果预览:

可通过限制手势移动阈值区分点击、滑动事件。
import { PromptAction } from '@kit.ArkUI'; @Entry @Component struct Index { promptAction: PromptAction = this.getUIContext().getPromptAction(); build() { Column() { Swiper() { Text('公告') .width('80%') .fontSize(20) .textAlign(TextAlign.Center) .onClick(() => { this.promptAction.showToast({ message: '通知详情', alignment: Alignment.Top }); }, 1); } .height('20%') .vertical(true) .indicator(false); } .width('100%') .height('100%'); } }
效果预览:

import { PromptAction } from '@kit.ArkUI'; @Entry @Component struct TapGestureExample { promptAction: PromptAction = this.getUIContext().getPromptAction(); build() { Column() { Swiper() { // 单指双击文本触发手势事件 Text('Click twice').fontSize(28) .gesture( TapGesture({ count: 2, distanceThreshold: 50 }) .onAction((event: GestureEvent) => { if (event) { this.promptAction.showToast({ message: '通知详情', alignment: Alignment.Top }); } }) ); Text(''); }.indicator(false); } .height(200) .width(300) .padding(20) .margin(30); } }
效果预览:
