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

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

介绍

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

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

效果预览

使用说明

  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
          }
        }
      })
  )
相关推荐
Moment11 小时前
Vibe Coding 时代,到底该选什么样的工具来提升效率❓❓❓
前端·后端·github
IT_陈寒12 小时前
SpringBoot性能飙升200%?这5个隐藏配置你必须知道!
前端·人工智能·后端
小时前端13 小时前
React性能优化的完整方法论,附赠大厂面试通关技巧
前端·react.js
Nicko13 小时前
Jetpack Compose BOM 2026.02.01 解读与升级指南
前端
小蜜蜂dry13 小时前
nestjs学习 - 控制器、提供者、模块
前端·node.js·nestjs
优秀稳妥的JiaJi13 小时前
基于腾讯地图实现电子围栏绘制与校验
前端·vue.js·前端框架
前端开发呀14 小时前
从 qiankun(乾坤) 迁移到 Module Federation(模块联邦),对MF只能说相见恨晚!
前端
没想好d14 小时前
通用管理后台组件库-10-表单组件
前端
恋猫de小郭14 小时前
你用的 Claude 可能是虚假 Claude ,论文数据告诉你,Shadow API 中的欺骗性模型声明
前端·人工智能·ai编程
_Eleven14 小时前
Pinia vs Vuex 深度解析与完整实战指南
前端·javascript·vue.js