HarmonyOS Navigation实现Dialog转场动画

通过Navigation方案实现Dialog,发现不支持转场动画…,在Dialog模式的页面跳转Standard的页面也没有转场动画,有办法处理吗?不想自定义转场动画,因为跳转的页面由服务端下发,无法明确是目标页是哪个。

HarmonyOS
2024-12-25 09:09:06
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280

请参考下以下demo:

//Index.ets:
import { CustomDialogA, CustomDialogB } from '../cases/dialog/CustomDialog'
import { CaseDialog } from '../cases/dialog/CaseDialog'
import { AppRouter } from '../router/AppRouter'
@Entry
@Component
struct Index {
  @Provide('pageInfos') pageInfos: NavPathStack = new NavPathStack()
  aboutToAppear() {
    AppRouter.getInstance().register(this.pageInfos);
  }
  @Builder
  PageMap(name: string) {
    if (name === 'dialog') {
      CaseDialog()
    } else if (name === 'customDialog') {
      CustomDialogA()
    } else if (name === 'fullDialog') {
      CustomDialogB()
    }
  }
  build() {
    Navigation(this.pageInfos) {
      Column() {
        Button('Dialog', { stateEffect: true, type: ButtonType.Capsule })
          .onClick(() => {
            this.pageInfos.pushPath({ name: 'dialog' })
          })
      }
    }
    .hideTitleBar(true)
    .navDestination(this.PageMap)
  }
}


//CaseDialog.ets:
import { AppRouter } from '../../router/AppRouter';
@Entry
@Component
export struct CaseDialog {
  @State message: string = "弹窗背景";
  build() {
    NavDestination() {
      Column() {
        Text(this.message)
        Row()
          .height("90%")
          .width("100%")
          .backgroundColor(Color.Brown)
        Button('打开弹窗', { stateEffect: true, type: ButtonType.Capsule })
          .onClick(() => {
            // 打开组件弹窗
            AppRouter.openDialog("customDialog");
          })
      }
    }
  }
}


//CustomDialog.ets:
import { AppRouter, RouterParams } from '../../router/AppRouter';
@Component
export struct CustomDialogA {
  @State heightSize: string = "50%";
  @State params: string = "";
  aboutToAppear() {
    this.params = AppRouter.getParams("customDialog");
  }
  build() {
    NavDestination() {
      Stack({ alignContent: Alignment.Bottom }) {
        Column() {}
        .width("100%")
        .height("100%")
        .backgroundColor('rgba(0,0,0,0.5)')
        .transition(
          TransitionEffect.OPACITY.animation({
            duration: 300,
            curve: Curve.Friction
          })
        )
        .onClick(() => {
          AppRouter.pop();
        })
        Column() {
          Text("弹窗")
            .fontColor(Color.White)
          Text("参数:" + this.params)
            .fontSize(18)
            .fontColor(Color.White)
          Button("新弹窗", { stateEffect: true, type: ButtonType.Capsule })
            .onClick(() => {
              AppRouter.openDialog("fullDialog", { name: "new Open" } as RouterParams)
            })
        }
        .width("100%")
        .backgroundColor(Color.Blue)
        .height(this.heightSize)
        // .align(Alignment.Bottom)
        .transition(
          TransitionEffect.move(TransitionEdge.BOTTOM)
            .animation({
              duration: 300,
              curve: Curve.Friction
            })
        )
      }
      .width("100%")
      .height("100%")
    }
    .mode(NavDestinationMode.DIALOG)
    .hideTitleBar(true)
  }
}
@Component
export struct CustomDialogB {
  @State params: string = "";
  aboutToAppear() {
    this.params = AppRouter.getParams("fullDialog");
  }
  build() {
    NavDestination() {
      Column() {
        Text("全屏弹窗")
        Text("参数:" + this.params)
          .fontSize(18)
        Button("返回", { stateEffect: true, type: ButtonType.Capsule })
          .onClick(() => {
            AppRouter.pop()
          })
      }
      .backgroundColor(Color.White)
      .width("100%")
      .height("100%")
      // .align(Alignment.Bottom)
      .transition(
        TransitionEffect.move(TransitionEdge.END)
          .animation({
            duration: 300,
            curve: Curve.Friction
          })
      )
    }
    .mode(NavDestinationMode.DIALOG)
    .hideTitleBar(true)
    .transition(
      TransitionEffect.move(TransitionEdge.END)
        .animation({
          duration: 300,
          curve: Curve.Friction
        })
    )

  }
}


//AppRouter.ets:
import { HashMap } from '@kit.ArkTS';
export interface RouterParams {
  name?: string
}
export class AppRouter {
  private pageStack?: NavPathStack;
  private routeMap: HashMap<string, WrappedBuilder<[object]>> = new HashMap();
  public static getInstance(): AppRouter {
    return instance;
  }
  register(pageStack: NavPathStack): void {
    this.pageStack = pageStack;
  }
  public addRouteMap(name: string, builder:
    WrappedBuilder<[object]>): void {
    this.routeMap.set(name, builder)
  }
  private pushPath(name: string): void {
    this.pageStack!.pushPath({ name: name })
  }
  public static push(name: string): void {
    instance.pushPath(name);
  }
  public static openDialog(name: string, params?: RouterParams): void {
    instance.pageStack?.pushPath({ name: name, param: params });
  }
  public static replace(name: string, params?: RouterParams): void {
    instance.pageStack?.replacePath({ name: name, param: params });
  }
  public static getParams(name: string): string {
    return JSON.stringify(instance.pageStack?.getParamByName(name));
  }
  public static pop(): void {
    instance.pageStack?.pop();
  }
}
let instance: AppRouter = new AppRouter();
export interface AnimateCallback {
  finish: ((isPush: boolean, isExit: boolean) => void | undefined) | undefined;
  start: ((isPush: boolean, isExit: boolean) => void | undefined) | undefined;
  onFinish: ((isPush: boolean, isExit: boolean) => void | undefined) | undefined;
  timeout: (number | undefined) | undefined;
}
export class CustomTransition {
  static delegate = new CustomTransition();
  customTransitionMap: Map<string, AnimateCallback> = new Map()
  static getAnimateParam(name: string): AnimateCallback {
    let result: AnimateCallback = {
      start: CustomTransition.delegate.customTransitionMap.get(name)?.start,
      finish: CustomTransition.delegate.customTransitionMap.get(name)?.finish,
      timeout: CustomTransition.delegate.customTransitionMap.get(name)?.timeout,
      onFinish: CustomTransition.delegate.customTransitionMap.get(name)?.onFinish
    };
    return result;
  }
  static registerNavParam(name: string, startCallback: (operation: boolean, isExit: boolean) => void,
    endCallback: (operation: boolean, isExit: boolean) => void,
    onFinish: (opeation: boolean, isExit: boolean) => void, timeout: number): void {
    if (CustomTransition.delegate.customTransitionMap.has(name)) {
      let param = CustomTransition.delegate.customTransitionMap.get(name);
      if (param != undefined) {
        param.start = startCallback;
        param.finish = endCallback;
        param.timeout = timeout;
        param.onFinish = onFinish;
        return;
      }
    }
    let params: AnimateCallback = {
      timeout: timeout,
      start: startCallback,
      finish: endCallback,
      onFinish: onFinish
    };
    CustomTransition.delegate.customTransitionMap.set(name, params);
  }
}
  • 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.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237.
分享
微博
QQ
微信
回复
2024-12-25 10:55:21


相关问题
HarmonyOS navigation导航转场动画怎么写
762浏览 • 1回复 待解决
HarmonyOS Navigation转场动画的一些思路
727浏览 • 1回复 待解决
Navigation如何自定义立体转场动画
342浏览 • 1回复 待解决
如何实现动画转场效果
1790浏览 • 1回复 待解决
HarmonyOS Navigation转场?
524浏览 • 0回复 待解决
HarmonyOS SideBarContainer 转场动画
572浏览 • 1回复 待解决
HarmonyOSNavigation显示dialog问题
1430浏览 • 1回复 待解决
HarmonyOS 页面内的组件转场动画
1121浏览 • 1回复 待解决
HarmonyOS Refresh和页面转场动画demo
685浏览 • 1回复 待解决
HarmonyOS 无感转场动画推荐方案
869浏览 • 1回复 待解决