HarmonyOS 实现按钮长按动画效果?

怎么实现一个按钮长按+动画效果

阅读 573
1 个回答

类似效果可以参考以下代码

// ets
@Entry
@Component
struct Progress_Ring {
  @State progressValue: number = 0
  @State animationId: number | null = null
  build() {
    Column({ space: 15 }) {
      Stack(){
        Progress({ value: 0, total: 100, type: ProgressType.Ring })
          .color("red")
          .value(this.progressValue)
          .width(120)
          .style({ strokeWidth: 5, scaleCount: 20, scaleWidth: 5, enableSmoothEffect: true })
          .backgroundColor('#00000000')

        Button('test').gesture(
          LongPressGesture({ repeat: true,duration:10 })
            .onAction((event: GestureEvent|undefined) => {
              if(event && this.progressValue<=100){
                clearInterval(this.animationId)
                this.animationId = null
                this.progressValue ++
              }
            })
            .onActionEnd(() => {
              this.animationId = setInterval(()=>{
                this.progressValue --
                if(this.progressValue <=0){
                  clearInterval(this.animationId)
                }
              },10)
            })
        )
      }

    }.width('100%').padding({ top: 5 })
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进