HarmonyOS 组件显示在下方,想实现一个动画效果 显示时 从底部向上弹出,关闭时 ,向下收回关闭的动画?

组件显示在下方,想实现一个动画效果,显示时从底部向上弹出动画效果,关闭时,向下收回关闭的动画,请问这2个动画咋写呢?

阅读 523
1 个回答

可以参考下demo用transition动画去实现

// 弹窗交互
@CustomDialog
@Component
struct CustomDialogExample {

  controller: CustomDialogController = new CustomDialogController({
    builder: CustomDialogExample({}),
    autoCancel:false
  })

  @State showFlag: Visibility = Visibility.Visible;
  @State isAutoCancel: boolean = false;

  build() {
    Column() {
      Text('我是内容').fontSize(20).margin({ top: 10, bottom: 10 })
        .backgroundColor('#FFFFFF')
        .textAlign(TextAlign.Center)
        .height(200)
        .width('100%')
        .margin({top:600})
        .onClick(()=>{
          this.cancel();
        })
    }
    .width('100%')
    .height('100%')
    .margin({
      bottom: -15
    })
    .onClick(()=>{
      if(this.isAutoCancel){
        this.cancel();
      }
    })
    .visibility(this.showFlag)
    .transition(TransitionEffect.OPACITY.animation({duration: 1000}).combine(TransitionEffect.translate({y: 100})))
  }

  cancel(){
    this.showFlag=Visibility.Hidden
    setTimeout(()=>{
      this.controller.close()
    }, 1000)
  }
}

@Entry
@Component
struct CustomDialogUser {
  @State isAutoCancel: boolean = false;

  dialogController: CustomDialogController = new CustomDialogController({
    builder: CustomDialogExample({ isAutoCancel: this.isAutoCancel }),
    autoCancel: this.isAutoCancel,
    customStyle:true
  })

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