实现弧形菜单功能鸿蒙示例代码

本文原创发布在华为开发者社区

介绍

本示例介绍弧形菜单的场景化案例。

实现弧形菜单功能源码链接

效果预览

使用说明

  1. 进入应用会立马出现弧形菜单。
  2. 可通过滑动进行菜单旋转。

实现思路

弧形菜单展示,通过手势旋转菜单位置。核心代码如下,源码参考Index.ets

typescript 复制代码
Column() {
  Stack() {
    ForEach(this.imageList, (item: string, index: number) => {
      Image($r(item))
        .width(this.ImageWidth)
        .height(this.ImageHeight)
        .border({ color: "#FFF", width: 2 })
        .position({
          x: this.points[index][0],
          y: this.points[index][1]
        })
        .objectFit(ImageFit.Auto)
        .rotate({ centerX: 100 / 2, centerY: 200 / 2, angle: this.getAngele(index) })
        .zIndex(this.imageList.length - index)
        .id(`${index}`)
    })
  }
  //centerY = 图片高度 + 圆的半径
  .rotate({ angle: this.stackAngle, centerY: 200 + 100 })
}.height('100%')
  .width('100%')
  .backgroundColor("#ffa7a7a7")
  .gesture(
    PanGesture(this.panOption)
      .onActionStart((event: GestureEvent) => {
        Logger.info('Pan start ' + event.offsetX)
        let info = event.fingerList[0]
        this.x1 = px2vp(this.screenWidth) / 2 - info.globalX
        this.y1 = 200 + 100 - info.globalY
        Logger.info('Pan start ' + this.x1 + ' ' + this.y1)
      })
      .onActionEnd((event: GestureEvent) => {
        Logger.info('Pan start ' + event.offsetY)
        Logger.info('Pan start ' + this.x2 + ' ' + this.y2)
      })
      .onActionUpdate((event: GestureEvent) => {
        Logger.info('this.stackAngle = ' + this.stackAngle)
        if (event) {
          let info = event.fingerList[0]
          this.x2 = px2vp(this.screenWidth) / 2 - info.globalX
          this.y2 = 200 + 100 - info.globalY
          this.distance = Math.sqrt((event.offsetX) * (event.offsetX) + (event.offsetY) * (event.offsetY))
          if ((this.x1 * this.y2 - this.x2 * this.y1) > 0) {
            this.stackAngle = (this.stackAngle + (this.distance / 60)) % 360
          } else {
            this.stackAngle = (this.stackAngle - (this.distance / 60)) % 360
          }
        }
      })
  )
相关推荐
9ilk16 分钟前
【前端基础】--- HTML
前端·html
Lafar17 分钟前
Dart单线程怎么保证UI运行流畅
前端·面试
不和乔治玩的佩奇23 分钟前
【 设计模式】常见前端设计模式
前端
bloxed29 分钟前
vue+vite 减缓首屏加载压力和性能优化
前端·vue.js·性能优化
打野赵怀真42 分钟前
React Hooks 的优势和使用场景
前端·javascript
HaushoLin1 小时前
ERR_PNPM_DLX_NO_BIN No binaries found in tailwindcss
前端·vue.js·css3·html5
Lafar1 小时前
Widget 树和 Element 树和RenderObject树是一一 对应的吗
前端
小桥风满袖1 小时前
炸裂,前端神级动效库合集
前端·css
匆叔1 小时前
Tauri 桌面端开发
前端·vue.js