Image组件无法设置长按事件

最近对image 设置长按事件,通过api发现有个长按事件  LongPressGesture,但是使用了长按没反应,于是看文档,文档描述如下:

当组件默认支持可拖拽时,如Text、TextInput、TextArea、HyperLink、Image和RichEditor等组件。长按手势与拖拽会出现冲突,事件优先级如下:

长按触发时间 < 500ms,长按事件优先拖拽事件响应。

长按触发时间 >= 500ms,拖拽事件优先长按事件响应。

那心想是不是事件冲突,为了保证长按事件可以,禁掉别的事件是不是就可以,经过验证是可以的,设置draggable(false)就ok了。

@Entry
@Component
export struct FirstPage {
  build() {
    Column() {
      Text("测试image长按事件").margin({ bottom: 15 })
      Image($r('app.media.app_icon'))
        .width(60)
        .height(60)
        .draggable(false) //Image组件默认是可拖拽的,给Image组件设置draggable为false
        .gesture(
          LongPressGesture({ repeat: true })
            .onAction((event?: GestureEvent) => {
              if (event) {
                console.log(`FirstPage 收到了事件了吗`)
              }
            })
        )
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
  }
}

猜你喜欢

转载自blog.csdn.net/ai_yong_jie/article/details/139527660