HarmonyOS鸿蒙Next中【快应用】任意拖动图标实现案例

HarmonyOS鸿蒙Next中【快应用】任意拖动图标实现案例 问题背景:

快应用页面开发阶段,ui布局时总是会遇到要在页面上实现一个可以任意拖动的导航栏,且在拖动时不能超出屏幕和导航栏不能在到边界时被压缩。一些开发者就会被困住了,这里就介绍一个实现导航栏的一个简易方式。

方案:

  1. 通过block实现组件堆叠效果,使得image图标位于list组件上方,并将image的样式设置为“position: fixed”。
  2. 通过监听image组件的touchmove触摸事件实现动态设置其位置,具体可参见“通用事件”中的“touchmove事件”和“TouchEvent类型说明”。
  3. device.getInfoSync获取页面的宽高来设置边界,保证导航栏不会滑到屏幕外。

实现代码:

<template>
  <div class="item-container">
    <block>
      <div class="container">
        <list class="list" style="height: 100px" for="{{this.dataList}}">
          <list-item class="list-item" type="content">
            <text>{{ $item.name }}</text>
          </list-item>
        </list>
      </div>
      <image id="img" style="position: fixed;left: {{this.clientX}};top:{{this.clientY}}" class="image" src="/Common/logo.png" ontouchmove="touchmove"></image>
    </block>
  </div>
</template>

<style>
  .item-container {
    margin-bottom: 50px;
    flex-direction: column;
  }
  .container {
    flex-direction: column;
  }
  .list {
    flex-direction: column;
  }
  .image {
    border-radius: 100px;
    height: 150px;
  }
  .list-item {
    border: 1px solid #c21f1f;
  }
</style>

<script>
  import device from '@system.device';
  export default {
    data: {
      clientX: "",
      clientY: "",
      width: 0,
      height: 0,
      dataList: [
        { name: "Language" },
        { name: "Maths" },
        { name: "English" },
        { name: "PE" },
        { name: "History" },
        { name: "Geography" },
        { name: "Science" },
        { name: "Physics" },
        { name: "Chemical" },
        { name: "Biology" },
        { name: "Music" },
        { name: "Art" },
        { name: "Language1" },
        { name: "Maths1" },
      ]
    },
    onInit: function () {
      this.$page.setTitleBar({ text: "Long press to drag the icon" });
      console.info("Application onInit");
    },
    onShow(options) {
      console.info("Application onShow");
      const res = device.getInfoSync();
      this.width = res.windowLogicWidth
      this.height = res.windowLogicHeight
      console.info("hjj", typeof this.width);
    },
    onHide(options) {
      console.info("Application onHide");
    },
    touchmove(TouchEvent) {
      console.log(JSON.stringify(TouchEvent.changedTouches));
      this.clientX = (TouchEvent.changedTouches[0].clientX - 75) + "px"
      this.clientY = (TouchEvent.changedTouches[0].clientY - 75) + "px"
      console.log("clientX = " + TouchEvent.changedTouches[0].clientX);
      console.log("clientY = " + TouchEvent.changedTouches[0].clientY);
      if ((TouchEvent.changedTouches[0].clientX - 75) <= 0) {
        this.clientX = 0 + "px"
      }
      if ((TouchEvent.changedTouches[0].clientY - 75) <= 0) {
        this.clientY = 0 + "px"
      }
      if ((TouchEvent.changedTouches[0].clientX) >= this.width - 75) {
        this.clientX = 600 + "px"
      }
      if ((TouchEvent.changedTouches[0].clientY) >= this.height - 75) {
        this.clientY = 1230 + "px"
      }    
    }
  };
</script>

更多关于HarmonyOS鸿蒙Next中【快应用】任意拖动图标实现案例的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于HarmonyOS鸿蒙Next中【快应用】任意拖动图标实现案例的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,实现快应用图标的任意拖动功能,可以通过以下步骤实现:

  1. 布局设置:使用<div><image>元素作为图标容器,并为其设置position: absolute,以便自由定位。

  2. 事件监听:为图标元素绑定touchstarttouchmovetouchend事件,监听用户的触摸操作。

  3. 位置更新:在touchmove事件中,获取触摸点的坐标,并实时更新图标的位置,通过修改lefttop属性实现拖动效果。

  4. 边界处理:确保图标在拖动过程中不超出屏幕边界,进行边界检测和限制。

通过以上步骤,即可在快应用中实现图标的任意拖动功能。

回到顶部