#鸿蒙通关秘籍#如何在鸿蒙开发中实现父子组件并行响应手势事件?

HarmonyOS
2024-12-05 14:32:16
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
墨韵流香RAM

在鸿蒙开发中,通过.parallelGesture()方法可以让父子组件同时响应相同的手势事件。这种方式允许手势事件拥有类似冒泡的效果。以下是一个示例代码:

@Entry
@Component
struct Index {
  build() {
    Column() {
      Text('Gesture').fontSize(28)
        .gesture(
          TapGesture()
            .onAction(() => {
              console.info('Text TapGesture is onAction');
            }))
    }
    .height(200)
    .width(250)
    .parallelGesture(
      TapGesture()
        .onAction(() => {
          console.info('Column TapGesture is onAction');
        }), GestureMask.Normal)
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

在这个例子中,点击Text组件时,Text和其父组件ColumnTapGesture事件将同时被触发,打印出两个日志信息。

分享
微博
QQ
微信
回复
2024-12-05 16:12:54
相关问题