鸿蒙自定义DrawerLayout侧滑菜单实现原理

前言

DevEcoStudio版本:

DrawerLayout如何使用:鸿蒙自定义侧滑菜单布局(DrawerLayout)-CSDN博客

实现原理

1、Library创建

新建module选择static library,命名为DrawerLibrary

2、DrawerLayout原理分析

import { DrawerController } from './DrawerController'

/**
 * 自定义抽屉布局
 */
@Component
export struct DrawerLayout {
   @State offsetX: number = 0
   //缩放比例
   @State scaleX: number = 1
   //是否显示侧边栏
   @Link isShowSideBar: boolean
   //侧边栏的宽度
   private sideBarWidth: number = 300
   // 按下的x坐标
   private downX = 0
   @BuilderParam
   leftView: () => void
   @BuilderParam
   rightView: () => void
   //控制器
   @Link controller: DrawerController;

   aboutToAppear() {
      if (this.isShowSideBar) {
         this.offsetX = this.sideBarWidth
         this.scaleX = 0.8
      } else {
         this.offsetX = 0
         this.scaleX = 1
      }
      this.controller.showSideBar = () => {
         this.showSideBar();
      };
      this.controller.hideSideBar = () => {
         this.hideSideBar();
      };
   }

   build() {
      Row() {
         Stack() {
            this.leftView()
         }
         .backgroundColor(Color.White)
         .width(this.sideBarWidth)
         .height('100%')
         .offset({ x: this.offsetX - this.sideBarWidth, y: 0 })

         Stack() {
            this.rightView()
         }
         .backgroundColor(Color.White)
         .width(`${this.scaleX * 100}%`)
         .height(`${this.scaleX * 100}%`)
         .margin({ left: (1 - this.scaleX) * 180 })
         .offset({ x: this.offsetX - this.sideBarWidth, y: 0 })
         .onClick(() => {
            this.controller.hideSideBar()
         })
      }
      // .onTouch((event) => this.touchEvent(event)) //留的手指滑动的口子,大家根据下面原理自己实现
      .backgroundColor('#CCCCCCCC')
      .width('100%')
      .height('100%')
   }

 
   /**
    * 显示侧边栏
    */
   private showSideBar() {
      animateTo({ duration: 300 }, () => {
         this.offsetX = this.sideBarWidth
         this.scaleX = 0.8
         this.isShowSideBar = true
      })
   }

   /**
    * 隐藏侧边栏
    */
   private hideSideBar() {
      animateTo({ duration: 300 }, () => {
         this.offsetX = 0
         this.scaleX = 1
         this.isShowSideBar = false
      })
   }
}
复制代码
DrawerController类:
/**
 * 抽屉布局控制器
 */
export class DrawerController {
   showSideBar: () => void;
   hideSideBar: () => void;
}

a:偏移量

通过offset({ x: this.offsetX - this.sideBarWidth, y: 0 })动态调整左右视图在x轴方向的偏移量,有两种状态:显示和隐藏侧边栏,在显示状态下通过animateTo动画将offsetX=0在300ms内改变成offsetX = sideBarWidth(侧边栏宽度),同理在隐藏状态下通过animateTo动画将offsetX = sideBarWidth(侧边栏宽度)在300ms内改变成 offsetX = 0。

b:右侧内容缩放

通过动态调整右侧内容的width和height值来实现缩放效果。在显示状态下通过animateTo动画将scaleX = 1在300ms内改变成scaleX = 0.8,同理在隐藏状态下通过animateTo动画将scaleX = 0.8在300ms内改变成 scaleX = 1。

.width(`${this.scaleX * 100}%`)
.height(`${this.scaleX * 100}%`)

c:右侧内容距离左侧侧边栏的距离

通过动态调整右侧内容的的外边距左侧距离margin({ left: (1 - this.scaleX) * 180 })

通过以上的原理就能实现侧边栏效果了。

思考:

上面的实现原理没有实现通过手指滑动来控制侧边栏的隐藏和显示,通过下图的示意图分析下了手指滑动的原理,这里给大家留一个自己动手的机会,自己实现下如何通过手指控制侧边栏的显示和隐藏。

提示下:通过给最外层Row()设置触摸监听

复制代码
Row(){}.onTouch((event) => this.touchEvent(event))
相关推荐
openinstall全渠道统计5 小时前
免填邀请码工具:赋能六大核心场景,重构App增长新模型
android·ios·harmonyos
shaodong11235 小时前
鸿蒙系统-同应用跨设备数据同步(分布式功能)
分布式·华为·harmonyos
敢嗣先锋5 小时前
鸿蒙5.0实战案例:基于ImageKit对图片进行处理
移动开发·harmonyos·arkui·组件化·鸿蒙开发
陈无左耳、5 小时前
HarmonyOS学习第3天: 环境搭建开启鸿蒙开发新世界
学习·华为·harmonyos
敢嗣先锋5 小时前
鸿蒙5.0实战案例:基于ArkUI的验证码实现
移动开发·harmonyos·openharmony·arkui·鸿蒙开发
别说我什么都不会8 小时前
鸿蒙轻内核M核源码分析系列十二 事件Event
操作系统·harmonyos
Huang兄9 小时前
鸿蒙-canvas-刮刮乐
华为·harmonyos
Sharknade9 小时前
鸿蒙-阻塞式文件锁
华为·harmonyos
敢嗣先锋9 小时前
鸿蒙5.0实战案例:基于原生能力的深色模式适配
ui·移动开发·harmonyos·arkui·组件化·鸿蒙开发
Huang兄9 小时前
鸿蒙-验证码输入框的几种实现方式-上
harmonyos