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

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

HarmonyOS
2024-05-26 11:40:12
985浏览
收藏 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 }) 
  } 
}
  • 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.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.

实现效果

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

适配的版本信息

SDK:4.0.10.10

IDE:DevEco Studio 4.0.3.600

分享
微博
QQ
微信
回复
2024-05-27 11:54:06


相关问题
Navigation如何自定义立体转场动画
287浏览 • 1回复 待解决
弹窗打开、关闭动画是否支持自定义
3321浏览 • 1回复 待解决
CustomDialog自定义动画
1328浏览 • 1回复 待解决
HarmonyOS 如何自定义导航转场
924浏览 • 1回复 待解决
HarmonyOS 自定义弹窗选择
1298浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog
748浏览 • 1回复 待解决
使用自定义弹窗实现分享弹窗
1343浏览 • 1回复 待解决
自定义弹窗使用相关问题
1719浏览 • 1回复 待解决
HarmonyOS 自定义弹窗的问题
1601浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog问题
1346浏览 • 1回复 待解决
HarmonyOS 自定义弹窗层级问题
952浏览 • 1回复 待解决
HarmonyOS 全局自定义弹窗demo
1187浏览 • 1回复 待解决
HarmonyOS 自定义弹窗控制问题
968浏览 • 1回复 待解决
HarmonyOS 自定义弹窗关闭问题
907浏览 • 1回复 待解决
HarmonyOS 使用全局自定义弹窗
768浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog 问题
775浏览 • 1回复 待解决