#鸿蒙通关秘籍#如何在鸿蒙开发中实现自定义的透明度渐变模态转场?

HarmonyOS
2024-12-05 14:09:34
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Y影刃EPC

通过在鸿蒙系统中设置modalTransitionModalTransition.ALPHA,可以实现透明度渐变的模态转场。这是一个完整的实现代码:

// xxx.ets
@Entry
@Component
struct ModalTransitionExample {
  @State isShow:boolean = false

  @Builder myBuilder() {
    Column() {
      Button("关闭模态")
        .margin(10)
        .fontSize(20)
        .onClick(()=>{
          this.isShow = false;
        })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }

  build() {
    Column() {
      Button("开启模态")
        .onClick(() => {
          this.isShow = true
        })
        .fontSize(20)
        .margin(10)
        .bindContentCover(this.isShow, this.myBuilder(), {
          modalTransition: ModalTransition.ALPHA, 
          backgroundColor: Color.Gray, 
          onWillAppear: () => {console.log("BindContentCover onWillAppear.")}, 
          onAppear: () => {console.log("BindContentCover onAppear.")}, 
          onWillDisappear: () => {console.log("BindContentCover onWillDisappear.")}, 
          onDisappear: () => {console.log("BindContentCover onDisappear.")}
        })
    }
    .justifyContent(FlexAlign.Center)
    .backgroundColor(Color.White)
    .width('100%')
    .height('100%')
  }
}
  • 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.

这个代码片段演示了如何使用ModalTransition.ALPHA来实现模态页面的透明度渐变转场,使得过渡更加平滑。

分享
微博
QQ
微信
回复
2024-12-05 16:08:54
相关问题
自定义颜色透明度如何实现
855浏览 • 1回复 待解决
HarmonyOS 如何实现透明度渐变效果
538浏览 • 1回复 待解决
SideBarContainer如何设置透明度
2767浏览 • 1回复 待解决