ts-animatorproperty.md 3.2 KB
Newer Older
E
esterzhou 已提交
1
# Property Animator
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
You can create a property animator to animate certain universal attributes of a component, including **width**, **height**, backgroundColor, **opacity**, **scale**, **rotate**, and **translate**.
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5 6
> **NOTE**
>
E
esterzhou 已提交
7
> This event is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8

E
ester.zhou 已提交
9
animation(value: {duration?: number, tempo?: number, curve?: string | Curve | ICurve, delay?:number, iterations: number, playMode?: PlayMode, onFinish?: () => void})
Z
zengyawen 已提交
10

E
ester.zhou 已提交
11
Applies a property animator to the component to animate its attributes over time.
Z
zengyawen 已提交
12

E
ester.zhou 已提交
13
**Parameters**
Z
zengyawen 已提交
14

Z
zengyawen 已提交
15

E
ester.zhou 已提交
16 17 18 19 20 21 22 23 24
| Name        | Type                                      | Mandatory   | Description                                                        |
| ---------- | ------------------------------------------| ---- | ------------------------------------------------------------ |
| duration   | number                                    | No   | Animation duration, in ms.<br>Default value: **1000**|
| tempo      | number                                    | No   | Animation playback speed. A greater value indicates a higher animation playback speed.<br>The value **0** indicates that no animation is applied.<br>Default value: **1**|
| curve      | string&nbsp;\|&nbsp;[Curve](ts-appendix-enums.md#curve)&nbsp;\|&nbsp;ICurve<sup>9+</sup> | No  | Animation curve.<br>Default value: **Curve.Linear**  |
| delay      | number                                    | No   | Delay of animation playback, in ms. The value **0** indicates that the playback is not delayed.<br>Default value: **0**  |
| iterations | number                                    | No   | Number of times that the animation is played. The value **-1** indicates that the animation is played for an unlimited number of times.<br>Default value: **1**|
| playMode   | [PlayMode](ts-appendix-enums.md#playmode) | No   | Animation playback mode. By default, the animation is played from the beginning after the playback is complete.<br>Default value: **PlayMode.Normal**|
| onFinish   | () => void                                | No   | Callback invoked when the animation playback is complete.                       |
Z
zengyawen 已提交
25 26 27


## Example
Z
zengyawen 已提交
28

E
ester.zhou 已提交
29 30
```ts
// xxx.ets
Z
zengyawen 已提交
31 32 33
@Entry
@Component
struct AttrAnimationExample {
E
ester.zhou 已提交
34 35 36
  @State widthSize: number = 200;
  @State heightSize: number = 100;
  @State flag: boolean = true;
Z
zengyawen 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

  build() {
    Column() {
      Button('click me')
        .onClick((event: ClickEvent) => {
          if (this.flag) {
            this.widthSize = 100
            this.heightSize = 50
          } else {
            this.widthSize = 200
            this.heightSize = 100
          }
          this.flag = !this.flag
        })
        .width(this.widthSize).height(this.heightSize).backgroundColor(0x317aff)
        .animation({
          duration: 2000, // Animation duration
          curve: Curve.EaseOut, // Animation curve
          delay: 500, // Animation delay
          iterations: 1, // Number of playback times
E
esterzhou 已提交
57 58
          playMode: PlayMode.Normal // Animation playback mode
        }) // Animation configuration for the width and height attributes of the <Button> component
Z
zengyawen 已提交
59 60 61 62 63
    }.width('100%').margin({ top: 5 })
  }
}
```

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