HarmonyOS Navigation实现Dialog转场动画

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

HarmonyOS
1天前
浏览
收藏 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);
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
如何实现动画转场效果
867浏览 • 1回复 待解决
HarmonyOS Navigation转场?
139浏览 • 0回复 待解决
HarmonyOSNavigation显示dialog问题
504浏览 • 1回复 待解决
HarmonyOS 页面内的组件转场动画
441浏览 • 1回复 待解决
HarmonyOS 无感转场动画推荐方案
115浏览 • 1回复 待解决
HarmonyOS Refresh和页面转场动画demo
28浏览 • 1回复 待解决
Tabs 出现/消失转场动画效果
409浏览 • 1回复 待解决
如何全局设置页面转场动画
763浏览 • 1回复 待解决