ts-page-transition-animation.md 11.0 KB
Newer Older
Z
zengyawen 已提交
1 2
# Page Transition

E
ester.zhou 已提交
3
You can customize the page entrance and exit animations in the **pageTransition** API for transition between pages. For details, see [Page Transition Animation](../../ui/arkts-page-transition-animation.md).
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5 6 7
> **NOTE**
>
> This event is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
E
ester.zhou 已提交
8
>
E
ester.zhou 已提交
9
> To achieve a better transition effect, you are advised to use the [\<Navigation>](../../ui/arkts-navigation-navigation.md) component and [modal transition](../../ui/arkts-modal-transition.md).
E
ester.zhou 已提交
10

Z
zengyawen 已提交
11

E
ester.zhou 已提交
12 13
| Name               | Parameter                                                        | Mandatory| Description                                                    |
| ------------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
E
ester.zhou 已提交
14 15
| PageTransitionEnter | {<br>type?: [RouteType](#routetype),<br>duration?: number,<br>curve?: [Curve](ts-appendix-enums.md#curve) \| string \| [ICurve](../apis/js-apis-curve.md#icurve)<sup>10+</sup>,<br>delay?: number<br>} | No  | Page entrance animation.<br>- **type**: route type for the page transition effect to take effect.<br>Default value: **RouteType.None**<br>- **duration**: animation duration.<br>Unit: ms<br>Default value: **1000**<br>- **curve**: animation curve. The value of the string type can be any of the following: "ease", "ease-in", "ease-out", "ease-in-out", "extreme-deceleration", "fast-out-linear-in", "fast-out-slow-in", "friction", "linear", "linear-out-slow-in", "rhythm", "sharp", "smooth".<br>Default value: **Curve.Linear**<br>- **delay**: animation delay.<br>Unit: ms<br>Default value: **0**<br>**NOTE**<br>If no match is found, the default page transition effect is used (which may vary according to the device). To disable the default page transition effect, set **duration** to **0**.|
| PageTransitionExit  | {<br>type?: [RouteType](#routetype),<br>duration?: number,<br>curve?: [Curve](ts-appendix-enums.md#curve) \| string \| [ICurve](../apis/js-apis-curve.md#icurve)<sup>10+</sup>,<br>delay?: number<br>} | No  | Page exit animation.<br>- **type**: route type for the page transition effect to take effect.<br>Default value: **RouteType.None**<br>- **duration**: animation duration.<br>Unit: ms<br>Default value: **1000**<br>- **curve**: animation curve. The value range of the string type is the same as that of **PageTransitionEnter**.<br>Default value: **Curve.Linear**<br>- **delay**: animation delay.<br>Unit: ms<br>Default value: **0**<br>**NOTE**<br>If no match is found, the default page transition effect is used (which may vary according to the device). To disable the default page transition effect, set **duration** to **0**.|
Z
zengyawen 已提交
16

E
ester.zhou 已提交
17
## RouteType
Z
zengyawen 已提交
18

E
ester.zhou 已提交
19 20
| Name| Description                                                        |
| ---- | ------------------------------------------------------------ |
E
ester.zhou 已提交
21 22 23
| Pop  | Redirects to a specified page. To redirect the user from page B back to page A, set **RouteType** of **PageTransitionExit** to **None** or **Pop** for page B and set **RouteType** of **PageTransitionEnter** to **None** or **Pop** for page A.|
| Push | Redirects to the next page. To redirect the user from page A to page B, set **RouteType** of **PageTransitionExit** to **None** or **Push** for page A and set **RouteType** of **PageTransitionEnter** to **None** or **Push** for page B.|
| None | The page is not redirected. The animation specified by **PageTransitionEnter** takes effect for page entrance, and the animation specified by **PageTransitionExit** takes effect for page exit.|
Z
zengyawen 已提交
24 25 26 27


## Attributes

E
ester.zhou 已提交
28 29
| Name | Type                                                    | Mandatory| Description                                                    |
| --------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
E
ester.zhou 已提交
30
| slide     | [SlideEffect](#slideeffect)                          | No  | Slide effect during page transition.|
E
ester.zhou 已提交
31
| translate | {<br>x? : number \| string,<br>y? : number \| string,<br>z? : number \| string<br>} | No  | Translation effect during page transition, which is the value of the start point of entrance and the end point of exit. When this parameter is set together with **slide**, the latter takes effect by default.<br>- **x**: translation distance along the x-axis.<br>- **y**: translation distance along the y-axis.<br>- **z**: translation distance along the y-axis.|
E
ester.zhou 已提交
32 33
| scale     | {<br>x? : number,<br>y? : number,<br>z? : number,<br>centerX? : number \| string,<br>centerY? : number \| string<br>} | No  | Scaling effect during page transition, which is the value of the start point of entrance and the end point of exit.<br>- **x**: scale ratio along the x-axis.<br>- **y**: scale ratio along the y-axis.<br>- **z**: scale ratio along the z-axis.<br>- **centerX** and **centerY**: scale center point. The default values are both **"50%"**, indicating that the center point of the page.<br>- If the center point is (0, 0), it refers to the upper left corner of the component.<br>|
| opacity   | number                                                       | No  | Opacity, which is the opacity value of the start point of entrance or the end point of exit.|
Z
zengyawen 已提交
34

E
ester.zhou 已提交
35
## SlideEffect
Z
zengyawen 已提交
36

E
ester.zhou 已提交
37 38 39 40 41 42
| Name    | Description                       |
| ------ | ------------------------- |
| Left   | When set to Enter, slides in from the left. When set to Exit, slides out to the left.|
| Right  | When set to Enter, slides in from the right. When set to Exit, slides out to the right.|
| Top    | When set to Enter, slides in from the top. When set to Exit, slides out to the top.|
| Bottom | When set to Enter, slides in from the bottom. When set to Exit, slides out to the bottom.|
Z
zengyawen 已提交
43 44 45


## Events
Z
zengyawen 已提交
46

E
ester.zhou 已提交
47
| Name                                                        | Description                                                    |
E
ester.zhou 已提交
48
| ------------------------------------------------------------ | ------------------------------------------------------------ |
E
ester.zhou 已提交
49 50
| onEnter(event: (type?: RouteType, progress?: number) =&gt; void) | Invoked once every animation frame until the entrance animation ends, when the value of **progress** changes from 0 to 1. The input parameter is the normalized progress of the current entrance animation. The value range is 0–1.<br>- **type**: route type.<br>- **progress**: current progress.<br> <br> |
| onExit(event: (type?: RouteType, progress?: number) =&gt; void) | Invoked once every animation frame until the exit animation ends, when the value of **progress** changes from 0 to 1. The input parameter is the normalized progress of the current exit animation. The value range is 0–1.<br>- **type**: route type.<br>- **progress**: current progress.<br> <br> |
Z
zengyawen 已提交
51 52 53


## Example
Z
zengyawen 已提交
54

E
ester.zhou 已提交
55
Example 1: Apply the entrance animation of fade-in and the exit animation of zoom-out.
Z
zengyawen 已提交
56

E
ester.zhou 已提交
57
```ts
Z
zengyawen 已提交
58 59 60 61
// index.ets
@Entry
@Component
struct PageTransitionExample1 {
E
ester.zhou 已提交
62 63 64
  @State scale1: number = 1
  @State opacity1: number = 1

Z
zengyawen 已提交
65
  build() {
E
ester.zhou 已提交
66
    Column() {
Z
zengyawen 已提交
67
      Navigator({ target: 'pages/page1', type: NavigationType.Push }) {
E
ester.zhou 已提交
68
        Image($r('app.media.bg1')).width('100%').height('100%')    // Store the image in the media folder.
Z
zengyawen 已提交
69
      }
E
ester.zhou 已提交
70
    }.scale({ x: this.scale1 }).opacity(this.opacity1)
Z
zengyawen 已提交
71
  }
E
ester.zhou 已提交
72
  // Customize the transition process.
Z
zengyawen 已提交
73 74 75
  pageTransition() {
    PageTransitionEnter({ duration: 1200, curve: Curve.Linear })
      .onEnter((type: RouteType, progress: number) => {
E
ester.zhou 已提交
76 77
        this.scale1 = 1
        this.opacity1 = progress
Z
zengyawen 已提交
78
      }) // The onEnter callback is triggered frame by frame during the entrance process. The input parameter is the normalized progress of the animation (0% to 100%).
E
ester.zhou 已提交
79
    PageTransitionExit({ duration: 1200, curve: Curve.Ease })
Z
zengyawen 已提交
80
      .onExit((type: RouteType, progress: number) => {
E
ester.zhou 已提交
81 82
        this.scale1 = 1 - progress
        this.opacity1 = 1
Z
zengyawen 已提交
83 84 85 86 87
      }) // The onExit callback is triggered frame by frame during the exit process. The input parameter is the normalized progress of the animation (0% to 100%).
  }
}
```

E
ester.zhou 已提交
88
```ts
Z
zengyawen 已提交
89 90 91 92
// page1.ets
@Entry
@Component
struct AExample {
E
ester.zhou 已提交
93 94 95
  @State scale2: number = 1
  @State opacity2: number = 1

Z
zengyawen 已提交
96 97
  build() {
    Column() {
E
ester.zhou 已提交
98 99
      Navigator({ target: 'pages/index', type: NavigationType.Push }) {
        Image($r('app.media.bg2')).width('100%').height('100%')   // Store the image in the media folder.
Z
zengyawen 已提交
100
      }
E
ester.zhou 已提交
101
    }.width('100%').height('100%').scale({ x: this.scale2 }).opacity(this.opacity2)
Z
zengyawen 已提交
102
  }
E
ester.zhou 已提交
103
  // Customize the transition process.
Z
zengyawen 已提交
104 105 106
  pageTransition() {
    PageTransitionEnter({ duration: 1200, curve: Curve.Linear })
      .onEnter((type: RouteType, progress: number) => {
E
ester.zhou 已提交
107 108
        this.scale2 = 1
        this.opacity2 = progress
Z
zengyawen 已提交
109
      }) // The onEnter callback is triggered frame by frame during the entrance process. The input parameter is the normalized progress of the animation (0% to 100%).
E
ester.zhou 已提交
110
    PageTransitionExit({ duration: 1200, curve: Curve.Ease })
Z
zengyawen 已提交
111
      .onExit((type: RouteType, progress: number) => {
E
ester.zhou 已提交
112 113 114
        this.scale2 = 1 - progress
        this.opacity2 = 1
      }) // The onExit callback is triggered frame by frame during the exit process. The input parameter is the normalized progress of the animation (0% to 100%).
Z
zengyawen 已提交
115 116 117 118
  }
}
```

E
ester.zhou 已提交
119
![pageTransition1](figures/pageTransition1.gif)
Z
zengyawen 已提交
120

E
ester.zhou 已提交
121
Example 2: Apply the entrance animation of sliding in from the left and the exit animation of translating with opacity change.
Z
zengyawen 已提交
122

E
ester.zhou 已提交
123
```ts
Z
zengyawen 已提交
124 125 126 127 128 129 130
// index.ets 
@Entry
@Component
struct PageTransitionExample {
  build() {
    Column() {
      Navigator({ target: 'pages/page1', type: NavigationType.Push }) {
E
ester.zhou 已提交
131
        Image($r('app.media.bg1')).width('100%').height('100%')   // Store the image in the media folder.
Z
zengyawen 已提交
132
      }
E
ester.zhou 已提交
133
    }
Z
zengyawen 已提交
134 135
  }

E
ester.zhou 已提交
136
  // Use the default effects provided by the system, such as translation, scaling, and opacity.
Z
zengyawen 已提交
137
  pageTransition() {
E
ester.zhou 已提交
138
    // Set the duration of the entrance animation to 1200 ms, in the purpose of matching the duration of the exit animation of the other page.
Z
zengyawen 已提交
139 140
    PageTransitionEnter({ duration: 1200 })
      .slide(SlideEffect.Left)
E
ester.zhou 已提交
141 142
    // Set the duration of the exit animation to 1000 ms, in the purpose of matching the duration of the entrance animation of the other page.
    PageTransitionExit({ duration: 1000 })
Z
zengyawen 已提交
143 144 145 146 147 148
      .translate({ x: 100.0, y: 100.0 })
      .opacity(0)
  }
}
```

E
ester.zhou 已提交
149
```ts
Z
zengyawen 已提交
150 151 152 153 154 155 156
// page1.ets
@Entry
@Component
struct PageTransitionExample1 {
  build() {
    Column() {
      Navigator({ target: 'pages/index', type: NavigationType.Push }) {
E
ester.zhou 已提交
157
        Image($r('app.media.bg2')).width('100%').height('100%')    // Store the image in the media folder.
Z
zengyawen 已提交
158
      }
E
ester.zhou 已提交
159
    }
Z
zengyawen 已提交
160 161
  }

E
ester.zhou 已提交
162
  // Use the default effects provided by the system, such as translation, scaling, and opacity.
Z
zengyawen 已提交
163
  pageTransition() {
E
ester.zhou 已提交
164 165
    // Set the duration of the entrance animation to 1000 ms, in the purpose of matching the duration of the exit animation of the other page.
    PageTransitionEnter({ duration: 1000 })
Z
zengyawen 已提交
166
      .slide(SlideEffect.Left)
E
ester.zhou 已提交
167 168
    // Set the duration of the exit animation to 1200 ms, in the purpose of matching the duration of the entrance animation of the other page.
    PageTransitionExit({ duration: 1200 })
Z
zengyawen 已提交
169 170 171 172 173 174
      .translate({ x: 100.0, y: 100.0 })
      .opacity(0)
  }
}
```

E
ester.zhou 已提交
175
![pageTransition2](figures/pageTransition2.gif)