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

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

HarmonyOS
2024-12-25 08:11:31
浏览
收藏 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)
}
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-25 11:10:45
相关问题
Navigation如何自定义立体转场动画
314浏览 • 1回复 待解决
自定义弹窗自定义转场动画
1949浏览 • 1回复 待解决
HarmonyOS navigation导航转场动画怎么
734浏览 • 1回复 待解决
HarmonyOS Navigation实现Dialog转场动画
711浏览 • 1回复 待解决
HarmonyOS 如何自定义导航转场
943浏览 • 1回复 待解决
如何实现自定义应用入场动画
1680浏览 • 1回复 待解决
CustomDialog自定义动画
1344浏览 • 1回复 待解决
swiper组件如何实现自定义切换动画
1977浏览 • 1回复 待解决
HarmonyOS Navigation转场动画一些思路
694浏览 • 1回复 待解决