自定义弹窗自定义转场动画

基于CustomDialog实现的自定义转场动画,如何从屏幕底部进场及向屏幕底部出场的转场动画。

HarmonyOS
2024-05-26 11:40:12
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
truemichael

使用的核心API

  • CustomDialog 自定义弹窗
  • transition 组件内转场动画

核心代码解释

let anmDuration: number = 800; 
// 弹窗交互 
@CustomDialog 
struct CustomDialogExample { 
  @State showFlag: Visibility = Visibility.Visible; 
  @State isAutoCancel: boolean = false; 
  
  controller: CustomDialogController = new CustomDialogController({ 
    builder: CustomDialogExample({}), 
    autoCancel: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: anmDuration}).combine(TransitionEffect.translate({y: 100}))) 
  } 
  
  // 延迟关闭弹窗,让自定义的出场动画显示 
  cancel(){ 
    this.showFlag=Visibility.Hidden 
    setTimeout(()=>{ 
      this.controller.close() 
    }, anmDuration) 
  } 
} 
  
@Entry 
@Component 
struct CustomDialogUser { 
  // 是否允许点击遮障层退出 
  @State isAutoCancel: boolean = true; 
  
  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 }) 
  } 
}

实现效果

能够实现自定弹窗定义自定义样式,让自定义弹窗能够根据定义动画展示进场出场动画。

适配的版本信息

SDK:4.0.10.10

IDE:DevEco Studio 4.0.3.600

分享
微博
QQ
微信
回复
2024-05-27 11:54:06
相关问题
弹窗打开、关闭动画是否支持自定义
616浏览 • 1回复 待解决
自定义弹窗如何嵌套使用
325浏览 • 1回复 待解决
自定义弹窗使用相关问题
302浏览 • 1回复 待解决
如何在自定义弹窗中再次弹窗
632浏览 • 1回复 待解决
如何设置自定义弹窗位置
611浏览 • 1回复 待解决
refresh期望能够自定义loading动画
438浏览 • 1回复 待解决
如何实现自定义应用入场动画
269浏览 • 1回复 待解决
如何去除自定义弹窗的白色背景
627浏览 • 1回复 待解决
自定义弹窗大小如何自适应内容
708浏览 • 1回复 待解决
自定义弹窗中的变量如何传递给页面
837浏览 • 1回复 待解决
如何理解自定义弹窗中的gridCount参数
752浏览 • 1回复 待解决
hvigor自定义扩展demo
201浏览 • 1回复 待解决