HarmonyOS Navigation页面转场的动画怎么自定义实现

比如想实现页面转场的淡入淡出效果

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

参考示例如下:

// 自定义动画对象,定义了Push、Pop、Replace各个动画阶段的回调函数,
export class CustomTransition {
  pageID : string = "invalidPageId";
  onPushInStart: () => void = () => {};
  onPushInEnd: () => void = () => {};
  onPushInFinish: () => void = () => {};
  onPopInStart: () => void = () => {};
  onPopInEnd: () => void = () => {};
  onPopInFinish: () => void = () => {};
  onReplaceInStart: () => void = () => {};
  onReplaceInEnd: () => void = () => {};
  onReplaceInFinish: () => void = () => {};
  onPushOutStart: () => void = () => {};
  onPushOutEnd: () => void = () => {};
  onPushOutFinish: () => void = () => {};
  onPopOutStart: () => void = () => {};
  onPopOutEnd: () => void = () => {};
  onPopOutFinish: () => void = () => {};
  onReplaceOutStart: () => void = () => {};
  onReplaceOutEnd: () => void = () => {};
  onReplaceOutFinish: () => void = () => {};
  curve: Curve | string = Curve.EaseInOut;

  // 获取启动阶段参数回调
  public getStart(operation : NavigationOperation, isInPage : boolean) : () => void {
    if (operation == NavigationOperation.PUSH) {
      if (isInPage) {
        return this.onPushInStart;
      } else {
        return this.onPushOutStart;
      }
    } else if (operation == NavigationOperation.POP) {
      if (isInPage) {
        return this.onPopInStart;
      } else {
        return this.onPopOutStart;
      }
    } else {
      if (isInPage) {
        return this.onReplaceInStart;
      } else {
        return this.onReplaceOutStart;
      }
    }
  }

  // 获取动画结束阶段参数回调
  public getEnd(operation : NavigationOperation, isInPage : boolean) : () => void {
    if (operation == NavigationOperation.PUSH) {
      if (isInPage) {
        return this.onPushInEnd;
      } else {
        return this.onPushOutEnd;
      }
    } else if (operation == NavigationOperation.POP) {
      if (isInPage) {
        return this.onPopInEnd;
      } else {
        return this.onPopOutEnd;
      }
    } else {
      if (isInPage) {
        return this.onReplaceInEnd;
      } else {
        return this.onReplaceOutEnd;
      }
    }
  }

  // 获取动画结束后参数回调
  public getFinished(operation : NavigationOperation, isInPage : boolean) : () => void {
    if (operation == NavigationOperation.PUSH) {
      if (isInPage) {
        return this.onPushInFinish;
      } else {
        return this.onPushOutFinish;
      }
    } else if (operation == NavigationOperation.POP) {
      if (isInPage) {
        return this.onPopInFinish;
      } else {
        return this.onPopOutFinish;
      }
    } else {
      if (isInPage) {
        return this.onReplaceInFinish;
      } else {
        return this.onReplaceOutFinish;
      }
    }
  }
}

// 自定义动画对象框架
export class CustomTransitionFW {
  // 各个页面自定义动画对象映射表
  private customTransitionMap: Map<string, CustomTransition> = new Map<string, CustomTransition>()

  private constructor() {

  }

  static instance = new CustomTransitionFW();

  static getInstance() {
    return CustomTransitionFW.instance;
  }

  registerNavParam(ct : CustomTransition): void {
    console.log("==== register customTransition Param for page : " + ct.pageID);
    if (this.customTransitionMap.has(ct.pageID)) {
      return;
    }
    this.customTransitionMap.set(ct.pageID, ct);
  }

  unRegisterNavParam(pageId: string): void {
    console.log("==== unregister customTransition Param for page : " +pageId);
    this.customTransitionMap.delete(pageId);
  }

  getAnimateParam(pageId: string): CustomTransition | undefined {
    if (this.customTransitionMap.get(pageId) == undefined) {
      console.error("==== not find CustomTransition for page : " + pageId);
      return undefined;
    }
    console.log("==== find CustomTransition for page : " + pageId);
    return this.customTransitionMap.get(pageId) as CustomTransition;
  }
}

// 动画目标界面
aboutToAppear(): void {
  this.info = {
    name: "MainPage"
  }
  if (this.info) {
  let ct : CustomTransition = new CustomTransition();
  ct.pageID = this.info.name.toString();
  console.log("==== MainPage pageId = " + ct.pageID);

  ct.onPushInStart = ct.onPushOutEnd = ct.onPopInStart = ct.onPopOutEnd = ct.onReplaceInStart = ct.onReplaceOutEnd = () => {
    this.transX = 300;
    console.log("==== MainPage ct.onPushInStart = ct.onPushOutEnd = ct.onPopInStart = ct.onPopOutEnd = ct.onReplaceInStart = ct.onReplaceOutEnd : " + this.transX)
    // this.transY = 500;
  }
  ct.onPushInEnd = ct.onPushOutStart = ct.onPopInEnd = ct.onPopOutStart = ct.onReplaceInEnd = ct.onReplaceOutStart = () => {
    this.transX = 0;
    console.log("==== MainPage ct.onPushInEnd = ct.onPushOutStart = ct.onPopInEnd = ct.onPopOutStart = ct.onReplaceInEnd = ct.onReplaceOutStart : " + this.transX)
    // this.transY = 0;
  }
  ct.onPushInFinish = ct.onPopInFinish = ct.onReplaceInFinish = () => {
    this.transX = 0;
    console.log("==== MainPage ct.onPushInFinish = ct.onPopInFinish = ct.onReplaceInFinish : " + this.transX)
    // this.transY = 0;
  }
  ct.onPushOutFinish = ct.onPopOutFinish = ct.onReplaceOutFinish = () => {
    this.transX = 300;
    console.log("==== MainPage ct.onPushOutFinish = ct.onPopOutFinish = ct.onReplaceOutFinish : " + this.transX)
    // this.transY = 0;
  }
  // 将页面的动画效果注册到动画框架中
  CustomTransitionFW.getInstance().registerNavParam(ct)
}
}
分享
微博
QQ
微信
回复
22h前
相关问题
自定义弹窗自定义转场动画
1165浏览 • 1回复 待解决
HarmonyOS Navigation实现Dialog转场动画
10浏览 • 0回复 待解决
HarmonyOS 如何自定义导航转场
12浏览 • 1回复 待解决
如何实现自定义应用入场动画
803浏览 • 1回复 待解决
swiper组件如何实现自定义切换动画
771浏览 • 1回复 待解决
CustomDialog自定义动画
439浏览 • 1回复 待解决
HarmonyOS 页面组件转场动画
438浏览 • 1回复 待解决
鸿蒙怎么实现自定义布局Dialog
9254浏览 • 2回复 已解决