目录

Transition Animation

Navigation provides default transition animations, as well as custom transition animations and shared element transition animations.

Default Transition Animations

The system provides multiple default transition types, which can be implemented through the NavDestination.systemTransition API. For details, see Setting Default Transition Animations for the NavDestination Component.

  • The default transition uses a spring curve. Its duration depends on the curve's physical parameters and may vary across devices. Therefore, the default animation duration is not controllable. Avoid coupling the default duration with service logic. To listen for animation completion, implement a custom transition.
  • Since API version 13, default transition animations are provided for the NavDestination component of the Dialog type. In earlier versions, such animations are not provided.

You can disable the default transition animations in either of the following ways:

Defining a Custom Transition

Navigation provides two levels of custom transition support: one for the entire Navigation component, and one for individual NavDestination pages.

When both customNavContentTransition and customTransition are used, customNavContentTransition takes precedence.

Defining a Shared Element Transition

You can implement shared element transitions between NavDestination components using geometryTransition. The following is an example. Ensure that the default transition animations are disabled for pages configured with shared element transitions. Otherwise, the default animations and shared element animations will be superimposed, resulting in an abnormal effect.

  1. Add the geometryTransition attribute to the components that need to implement shared element transitions, ensuring that the id parameter is consistent between the two NavDestination components.

    // Set id of the source page.
    NavDestination() {
      Column() {
        // ...
        // Replace $r('app.media.startIcon') with the actual resource file.
        Image($r('app.media.startIcon'))
          .geometryTransition('sharedId')
          .width(100)
          .height(100)
      }
    }.title('FromPage')
    // Set id of the destination page.
    NavDestination() {
      Column() {
        // Replace $r('app.media.startIcon') with the actual resource file.
        Image($r('app.media.startIcon'))
          .geometryTransition('sharedId')
          .width(200)
          .height(200)
      }
    }
    .title('ToPage')
  2. Wrap the navigation operation inside an animateTo closure, and then configure the animation parameters and disable the default transition.

    NavDestination() {
      Column() {
        // Replace $r('app.string.ToPage') with the actual resource file. The value in the resource file is "Go to target page."
    
        Button($r('app.string.ToPage'))
          .width('80%')
          .height(40)
          .margin(20)
          .onClick(() => {
            this.getUIContext()?.animateTo({ duration: 1000 }, () => {
              this.navPathStack.pushPath({ name: 'ToPage' }, false)
            });
          })
        // ...
      }
    }.title('FromPage')