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

鸿蒙场景化示例代码技术工程师
发布于 2025-4-15 11:01
3184浏览
0收藏

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

介绍

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

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

效果预览

实现弧形菜单功能鸿蒙示例代码-鸿蒙开发者社区

使用说明

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

实现思路

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


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
          }
        }
      })
  )

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
收藏
回复
举报
回复
    相关推荐