HarmonyOS鸿蒙Next中如何取消图片长按的默认响应,并添加自定义响应?

HarmonyOS鸿蒙Next中如何取消图片长按的默认响应,并添加自定义响应? 图片长按默认会被扩大并漂浮,如何取消默认动效并添加自定义响应?

@Entry
@Component
struct LongPressGestureExample {
  @State count: number = 0;

  build() {
    Column() {
      Text('LongPress onAction:' + this.count).fontSize(28)
      // 单指长按图片触发该手势事件
      Image('https://developer.huawei.com/images/foot/4app.png')
        .width(100)
        .objectFit(ImageFit.Cover)
        .gesture(
          LongPressGesture({ repeat: true })
          // 由于repeat设置为true,长按动作存在时会连续触发,触发间隔为duration(默认值500ms)
            .onAction((event: GestureEvent) => {
              if (event && event.repeat) {
                this.count++
              }
            })
            // 长按动作一结束触发
            .onActionEnd((event: GestureEvent) => {
              this.count = 0
            })
        )
      Text('长按图片')
    }
    .width(300)
    .padding(20)
    .border({ width: 3 })
    .margin(30)
  }
}

更多关于HarmonyOS鸿蒙Next中如何取消图片长按的默认响应,并添加自定义响应?的实战教程也可以访问 https://www.itying.com/category-93-b0.html

9 回复

取消默认动效并添加自定义响应的方案

图片长按时默认的“扩大并漂浮”动效通常与拖拽操作相关(如拖拽开始时的浮起预览效果)。

如果您的图片设置了可拖拽属性(.draggable(true)),则长按可能会触发系统默认的拖拽浮起动效。

如果您希望取消此默认动效并添加自定义响应(如计数或重置),需通过以下步骤实现:

1. 取消默认动效

  • 如果图片是可拖拽的(即设置了 .draggable(true)),您可以通过 禁用拖拽浮起动效 来取消默认的扩大漂浮效果。使用 dragPreviewOptions 属性,将 isLiftingDisabled 设置为 true

    Image(' https://developer.huawei.com/images/foot/4app.png ')
      .width(100)
      .objectFit(ImageFit.Cover)
      .draggable(true) // 如果图片需可拖拽,则设置此属性
      .dragPreviewOptions({}, {
        isLiftingDisabled: true // 禁用浮起效果
      })
      // 其他属性...
    
  • 如果图片不可拖拽(未设置 .draggable(true)),则长按不会触发默认拖拽动效,无需额外操作。

2. 添加自定义响应

  • 通过添加 长按手势LongPressGesture)并实现其回调函数来自定义响应行为。您的示例代码中已实现了自定义计数和重置功能:

    .gesture(
      LongPressGesture({ repeat: true })
        .onAction((event: GestureEvent) => {
          if (event && event.repeat) {
            this.count++ // 自定义动作:长按时连续计数
          }
        })
        .onActionEnd((event: GestureEvent) => {
          this.count = 0 // 自定义动作:长按结束重置计数
        })
    )
    

完整示例代码

结合上述两点,以下是修改后的代码示例,同时取消默认动效并添加自定义响应:

import { GestureEvent } from '@kit.ArkUI';

@Entry
@Component
struct LongPressGestureExample {
  @State count: number = 0;

  build() {
    Column() {
      Text('LongPress onAction:' + this.count).fontSize(28)
      Image(' https://developer.huawei.com/images/foot/4app.png ')
        .width(100)
        .objectFit(ImageFit.Cover)
        .draggable(true) // 如果不需要拖拽功能,可省略此行
        .dragPreviewOptions({}, {
          isLiftingDisabled: true // 禁用默认浮起动效
        })
        .gesture(
          LongPressGesture({ repeat: true })
            .onAction((event: GestureEvent) => {
              if (event && event.repeat) {
                this.count++ // 自定义响应:长按时增加计数
              }
            })
            .onActionEnd((event: GestureEvent) => {
              this.count = 0 // 自定义响应:长按结束重置计数
            })
        )
      Text('长按图片')
    }
    .width(300)
    .padding(20)
    .border({ width: 3 })
    .margin(30)
  }
}

注意事项

  • 如果图片不需要拖拽功能,请移除 .draggable(true),这样长按将仅触发手势事件,不会出现默认动效。
  • 长按手势的 repeat 参数设置为 true 时,会连续触发 onAction 回调(间隔默认500ms),您可根据需要调整。

更多关于HarmonyOS鸿蒙Next中如何取消图片长按的默认响应,并添加自定义响应?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


谢谢啊,不过修改前后的代码,都没有触发长按响应。

【背景知识】
手势响应:当用户的操作符合某个手势的特征时,系统会将其识别为该手势,这一过程称为手势识别。为了响应某一个手势,需在组件上添加对应的手势对象,以便系统可以收集并进行处理。

【参考方案】:
可参考视频拼接创作示例,使用List组件结合手势操作实现视频元素的拖动排序效果。

  • 使用List组件结合手势操作实现视频拖拽排序动效,拖拽排序同时更新视频拼接顺序。
List() {
  ForEach(this.list, (item: string, index: number) => {
    ListItem() {
      ... // 内部组件
    }
    .gesture(
       ... // 处理拖拽排序事件
    )
  }, (item: string) => {
    return item
  })
}

这个可用。

@Entry
@Component
struct LongPressGestureExample {
  @State count: number = 0;

  build() {
    Column() {
      Text('LongPress onAction:' + this.count).fontSize(28)
      // 单指长按图片触发该手势事件
      Image('https://developer.huawei.com/images/foot/4app.png')
        .width(100)
        .objectFit(ImageFit.Cover)
        .draggable(false) // 取消组件的拖拽能力
        .gesture(
          LongPressGesture({ repeat: true })
          // 由于repeat设置为true,长按动作存在时会连续触发,触发间隔为duration(默认值500ms)
            .onAction((event: GestureEvent) => {
              if (event && event.repeat) {
                this.count++
              }
            })
            // 长按动作一结束触发
            .onActionEnd((event: GestureEvent) => {
              this.count = 0
            })
        )
      Text('长按图片')
    }
    .width(300)
    .padding(20)
    .border({ width: 3 })
    .margin(30)
  }
}

解决方案

禁用系统默认拖拽行为:通过设置Image组件的draggable属性为false关闭默认拖拽效果

Image('https://...')
  .draggable(false) // 禁用系统默认长按拖拽动效
  .gesture(...)

添加自定义长按手势:使用LongPressGesture实现自定义响应逻辑,并通过参数控制触发频率

LongPressGesture({ repeat: true, duration: 600 })
  .onAction((event: GestureEvent) => {
    if (event.repeat) {
      // 自定义长按触发逻辑(如计数增加)
      this.count++
    }
  })
  .onActionEnd(() => {
    // 长按结束逻辑(如重置计数)
    this.count = 0
  })

代码优化

@Entry
@Component
struct LongPressGestureExample {
  @State count: number = 0;
  build() {
    Column() {
      Text('LongPress Count:' + this.count).fontSize(28)
      Image('https://...')
        .width(100)
        .objectFit(ImageFit.Cover)
        .draggable(false) // 关闭默认拖拽
        .gesture(
          LongPressGesture({ 
            repeat: true, // 允许持续触发
            duration: 600 // 设置触发间隔时间
          })
            .onAction((event: GestureEvent) => {
               if (event?.repeat) {
                 this.count++ // 自定义响应动作
               }
            })
            .onActionEnd(() => {
               this.count = 0 // 结束动作
            })
        )
    }
    .padding(20)
    .border({ width: 3 })
  }
}

通过设置draggable(false)关闭系统默认的长按拖拽效果

Image('https://developer.huawei.com/images/foot/4app.png')
 .draggable(false) // 关闭默认拖拽行为

结合LongPressGesture实现自定义响应逻辑

.gesture(
  LongPressGesture({ repeat: true })
    .onAction((event: GestureEvent) => {
      if (event?.repeat) { 
        this.count++ // 自定义长按触发逻辑
      }
    })
    .onActionEnd(() => {
      this.count = 0 // 结束时的重置逻辑
    })
)

ArkUI框架对以下组件实现了默认的拖拽能力,其中,Text、TextInput、TextArea、HyperLink、Image和RichEditor组件的draggable属性默认为true。
因为Image的draggabe属性默认为true,所以可以拖动,设置为false,可实现禁用组件的拖拽能力,同时也会取消图片长按默认被扩大并漂浮的动销。

示例代码如下:

@Entry
@Component
struct LongPressGestureExample {
  @State count: number = 0;

  build() {
    Column() {
      Text('LongPress onAction:' + this.count).fontSize(28)
      // 单指长按图片触发该手势事件
      Image('https://developer.huawei.com/images/foot/4app.png')
        .width(100)
        .objectFit(ImageFit.Cover)
        .draggable(false) // 取消组件的拖拽能力
        .gesture(
          LongPressGesture({ repeat: true })
          // 由于repeat设置为true,长按动作存在时会连续触发,触发间隔为duration(默认值500ms)
            .onAction((event: GestureEvent) => {
              if (event && event.repeat) {
                this.count++
              }
            })
            // 长按动作一结束触发
            .onActionEnd((event: GestureEvent) => {
              this.count = 0
            })
        )
      Text('长按图片')
    }
    .width(300)
    .padding(20)
    .border({ width: 3 })
    .margin(30)
  }
}

在HarmonyOS鸿蒙Next中,取消图片长按默认响应需使用onTouch事件监听并拦截LongPress手势。通过设置gesture组件的onAction回调返回true可阻止系统默认行为。添加自定义响应可在同一回调中实现自定义逻辑,例如弹窗或触发其他组件交互。

要取消图片长按的默认响应并添加自定义响应,可以通过以下方式实现:

  1. 取消默认动效:使用.interaction方法设置图片的交互行为为None,禁用系统默认的长按放大效果:
Image('your_image_url')
  .interaction(Interaction.None)
  1. 添加自定义长按手势:使用.gesture方法添加LongPressGesture,并在回调中实现自定义逻辑:
.gesture(
  LongPressGesture()
    .onAction((event: GestureEvent) => {
      // 自定义长按响应逻辑
      console.log('Long press detected');
    })
    .onActionEnd(() => {
      // 长按结束处理
    })
)

完整示例代码:

Image('https://example.com/image.png')
  .width(100)
  .interaction(Interaction.None) // 禁用默认交互
  .gesture(
    LongPressGesture()
      .onAction(() => {
        // 自定义长按行为
      })
  )

这样即可完全覆盖系统默认的长按行为,实现自定义响应。

回到顶部