ts-transition-animation-component.md 3.8 KB
Newer Older
Z
zengyawen 已提交
1
# Component Transition
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
Configure the component transition animations for when a component is inserted or deleted. This feature takes effect only when [animateTo](ts-explicit-animation.md) is used. The animation duration, curve, and delay are the same as those configured in **animateTo**.
Z
zengyawen 已提交
4

E
esterzhou 已提交
5 6 7
>  **NOTE**
>
>  The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8 9


Z
zengyawen 已提交
10
## Attributes
Z
zengyawen 已提交
11

E
esterzhou 已提交
12

E
ester.zhou 已提交
13 14 15 16 17 18 19
| Name| Type| Description|
| -------- | -------- | -------- |
| transition | TransitionOptions | Transition parameters, which are all optional. For details, see **TransitionOptions**.|

## TransitionOptions

| Name| Type| Mandatory| Description|
Z
zengyawen 已提交
20
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
21 22 23 24 25
| type | [TransitionType](ts-appendix-enums.md#transitiontype)  | No| Transition type, which includes component addition and deletion by default.<br>Default value: **TransitionType.All**<br>**NOTE**<br>If **type** is not specified, insertion and deletion use the same transition type.|
| opacity | number | No| Opacity of the component during transition, which is the value of the start point of insertion and the end point of deletion.<br>Default value: **1**|
| translate | {<br>x? : number,<br>y? : number,<br>z? : number<br>} | No| Translation of the component during transition, which is the value of the start point of insertion and the end point of deletion.<br>-**x**: distance to translate along the x-axis.<br>-**y**: distance to translate along the y-axis.<br>-**z**: distance to translate along the z-axis.|
| scale | {<br>x? : number,<br>y? : number,<br>z? : number,<br>centerX? : number,<br>centerY? : number<br>} | No| Scaling of the component during transition, which is the value of the start point of insertion and the end point of deletion.<br>- **x**: scale factor along the x-axis.<br>- **y**: scale factor along the y-axis.<br>- **z**: scale factor along the z-axis.<br>- **centerX** and **centerY**: x coordinate and y coordinate of the scale center, respectively.<br>- If the center point is 0, it indicates the upper left corner of the component.<br>|
| rotate | {<br>x?: number,<br>y?: number,<br>z?: number,<br>angle?: Angle,<br>centerX?: Length,<br>centerY?: Length<br>} | No| Rotation of the component during transition, which is the value of the start point of insertion and the end point of deletion.<br>- **x**: rotation vector along the x-axis.<br>- **y**: rotation vector along the y-axis.<br>- **z**: rotation vector along the z-axis.<br>- **centerX** and **centerY**: x coordinate and y coordinate of the rotation center, respectively.<br>- If the center point is (0, 0), it indicates the upper left corner of the component.|
Z
zengyawen 已提交
26 27


Z
zengyawen 已提交
28
## Example
Z
zengyawen 已提交
29 30 31

The following example shows how to use a button to control the appearance and disappearance of another button, and how to configure the required transition animations.

E
esterzhou 已提交
32 33
```ts
// xxx.ets
Z
zengyawen 已提交
34 35 36
@Entry
@Component
struct TransitionExample {
E
esterzhou 已提交
37
  @State btn: boolean = false
Z
zengyawen 已提交
38
  @State show: string = "show"
E
esterzhou 已提交
39

Z
zengyawen 已提交
40 41 42 43 44
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center,}) {
      Button(this.show).width(80).height(30).backgroundColor(0x317aff).margin({bottom:50})
        .onClick(() => {
          animateTo({ duration: 1000 }, () => {
E
esterzhou 已提交
45 46
            this.btn = !this.btn
            if(this.btn){
Z
zengyawen 已提交
47 48 49 50 51 52
              this.show = "hide"
            }else{
              this.show = "show"
            }
          })
        })
E
esterzhou 已提交
53
      if (this.btn) {
Z
zengyawen 已提交
54 55 56 57 58 59 60 61 62 63 64
        // The insertion and deletion have different transition effects.
        Button() {
          Image($r('app.media.bg1')).width("80%").height(300)
        }.transition({ type: TransitionType.Insert, scale : {x:0,y:1.0} })
        .transition({ type: TransitionType.Delete, scale: { x: 1.0, y: 0.0 } })
      }
    }.height(400).width("100%").padding({top:100})
  }
}
```

Z
zengyawen 已提交
65
![en-us_image_0000001211898498](figures/en-us_image_0000001211898498.gif)