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

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

介绍

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

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

效果预览

使用说明

  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
          }
        }
      })
  )
相关推荐
钛态6 小时前
Vite 中的 CSS 工程化:从 CSS Modules 到 UnoCSS 的渐进式迁移
前端·vue·react·web
Csvn8 小时前
原型链、this 与闭包内存:三座深水区一次打通(含内存泄漏排查实战)
前端·javascript
葡萄城技术团队8 小时前
如何通过 JSON 数据源 / Web API 快速构建轻量级报表(Report)?
前端·json·原型模式
浪兎兎8 小时前
【Java Web】Servlet + JSP 笔记
java·前端·servlet
Patrick_Wilson9 小时前
iOS Safari Clipboard API 权限弹窗问题
前端·ios·safari
OpenTiny社区10 小时前
Naive UI × GenUI SDK:自定义物料库搭建实战
前端·ai编程
kyriewen10 小时前
我拿 4 个真实前端任务试了 GLM-5.3 Flash:代码一遍跑通,账单 4 分钱
前端·程序员·ai编程
IT_陈寒10 小时前
Vue的响应式更新有时候真的不听话
前端·人工智能·后端
风骏时光牛马10 小时前
AI编程常见问题梳理与要点解析
前端
前端snow10 小时前
ai agent -- LCEL 汇总
前端