ts-animatorproperty.md 3.3 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 9


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

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

E
ester.zhou 已提交
14 15 16 17
| 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**|
E
ester.zhou 已提交
18
| curve      | string \| [Curve](ts-appendix-enums.md#curve) \| ICurve<sup>9+</sup> | No  | Animation curve.<br>Default value: **Curve.Linear**  |
E
ester.zhou 已提交
19 20 21 22
| 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 已提交
23 24 25


## Example
Z
zengyawen 已提交
26

E
ester.zhou 已提交
27 28
```ts
// xxx.ets
Z
zengyawen 已提交
29 30 31
@Entry
@Component
struct AttrAnimationExample {
E
ester.zhou 已提交
32 33 34 35
  @State widthSize: number = 250
  @State heightSize: number = 100
  @State rotateAngle: number = 0
  @State flag: boolean = true
Z
zengyawen 已提交
36 37 38

  build() {
    Column() {
E
ester.zhou 已提交
39 40
      Button('change width and height')
        .onClick(() => {
Z
zengyawen 已提交
41 42 43 44
          if (this.flag) {
            this.widthSize = 100
            this.heightSize = 50
          } else {
E
ester.zhou 已提交
45
            this.widthSize = 250
Z
zengyawen 已提交
46 47 48 49
            this.heightSize = 100
          }
          this.flag = !this.flag
        })
E
ester.zhou 已提交
50 51 52
        .margin(30)
        .width(this.widthSize)
        .height(this.heightSize)
Z
zengyawen 已提交
53
        .animation({
E
ester.zhou 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
          duration: 2000,
          curve: Curve.EaseOut,
          iterations: 3,
          playMode: PlayMode.Normal
        })
      Button('change rotate angle')
        .onClick(() => {
          this.rotateAngle = 90
        })
        .margin(50)
        .rotate({ angle: this.rotateAngle })
        .animation({
          duration: 1200,
          curve: Curve.Friction,
          delay: 500,
          iterations: -1,   // The value -1 indicates that the animation is played for an unlimited number of times.
          playMode: PlayMode.AlternateReverse
        })
    }.width('100%').margin({ top: 20 })
Z
zengyawen 已提交
73 74 75 76
  }
}
```

E
ester.zhou 已提交
77
![animation](figures/animation.gif)