#鸿蒙通关秘籍#如何实现HarmonyOS Next中左右拖动切换图片效果?

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
SKU风翼

要在HarmonyOS Next中实现左右拖动切换图片效果,通过创建三个Stack组件用于展示装修前后的图片对比,具体实现步骤如下:

  1. 创建三个Stack组件。

    • 第一个和第三个Stack用于存放两张装修图片,将zIndex设置为1。
    • 第二个Stack存放按钮图片,并将zIndex设置为2,使得按钮覆盖在图片上层。
  2. 将Image组件放置到Row容器中,并设置Row的宽度为状态变量,使用clip属性对Row进行裁剪。代码如下:

    Row() {
      Image($r('app.media.before_decoration'))
        .width($r('app.integer.decoration_width'))
        .height($r('app.integer.decoration_height'))
        .draggable(false)
      }
      .width(this.leftImageWidth)
      .clip(true)
      .zIndex(CONFIGURATION.ZINDEX1)
      .borderRadius({
        topLeft: $r('app.integer.borderradius'),
        bottomLeft: $r('app.integer.borderradius')
      })
    
  3. 为右侧Image组件加上direction属性,使其从右向左进行布局,并设置clip属性从左开始裁剪。

    Row() {
     Image($r('app.media.after_decoration'))
       .width($r('app.integer.decoration_width'))
       .height($r('app.integer.decoration_height'))
       .draggable(false)
    }
    .width(this.rightImageWidth)
    .clip(true)
    .zIndex(CONFIGURATION.ZINDEX1)
    .direction(Direction.Rtl)
    .borderRadius({
     topRight: $r('app.integer.borderradius'),
     bottomRight: $r('app.integer.borderradius')
    })
    
  4. 中间的按钮Image组件监听滑动手势事件,动态调整左右Image组件的宽度。

    Image($r('app.media.drag_button'))
      .width($r('app.integer.drag_button_image_width'))
      .height($r('app.integer.decoration_height'))
      .draggable(false)
      .gesture(
        PanGesture({ fingers: CONFIGURATION.PANGESTURE_FINGERS, distance: CONFIGURATION.PANGESTURE_DISTANCE })
          .onActionStart(() => {
            this.dragRefOffset = CONFIGURATION.INIT_VALUE;
          })
          .onActionUpdate((event: GestureEvent) => {
            this.dragRefOffset = event.offsetX;
            this.leftImageWidth = this.imageWidth + this.dragRefOffset;
            this.rightImageWidth = CONFIGURATION.IMAGE_FULL_SIZE - this.leftImageWidth;
            if (this.leftImageWidth >= CONFIGURATION.LEFT_IMAGE_RIGHT_LIMIT_SIZE) {
              this.leftImageWidth = CONFIGURATION.LEFT_IMAGE_RIGHT_LIMIT_SIZE;
              this.rightImageWidth = CONFIGURATION.RIGHT_IMAGE_RIGHT_LIMIT_SIZE;
            } else if (this.leftImageWidth <= CONFIGURATION.LEFT_IMAGE_LEFT_LIMIT_SIZE) {
              this.leftImageWidth = CONFIGURATION.LEFT_IMAGE_LEFT_LIMIT_SIZE;
              this.rightImageWidth = CONFIGURATION.RIGHT_IMAGE_LEFT_LIMIT_SIZE;
            }
          })
          .onActionEnd((event: GestureEvent) => {
            if (this.leftImageWidth <= CONFIGURATION.LEFT_IMAGE_LEFT_LIMIT_SIZE) {
              this.leftImageWidth = CONFIGURATION.LEFT_IMAGE_LEFT_LIMIT_SIZE;
              this.rightImageWidth = CONFIGURATION.RIGHT_IMAGE_LEFT_LIMIT_SIZE;
              this.imageWidth = CONFIGURATION.LEFT_IMAGE_LEFT_LIMIT_SIZE;
            } else if (this.leftImageWidth >= CONFIGURATION.LEFT_IMAGE_RIGHT_LIMIT_SIZE) {
              this.leftImageWidth = CONFIGURATION.LEFT_IMAGE_RIGHT_LIMIT_SIZE;
              this.rightImageWidth = CONFIGURATION.RIGHT_IMAGE_RIGHT_LIMIT_SIZE;
              this.imageWidth = CONFIGURATION.LEFT_IMAGE_RIGHT_LIMIT_SIZE;
            } else {
              this.leftImageWidth = this.imageWidth + this.dragRefOffset;
              this.rightImageWidth = CONFIGURATION.IMAGE_FULL_SIZE - this.leftImageWidth;
              this.imageWidth = this.leftImageWidth;
            }
          })
      )
    

通过以上步骤,实现了在HarmonyOS Next中左右拖动切换图片的效果。

分享
微博
QQ
微信
回复
1天前
相关问题