ts-animatorproperty.md 5.3 KB
Newer Older
E
ester.zhou 已提交
1
# Property Animation
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
You can animate certain properties 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 12
Since API version 9, this API is supported in ArkTS widgets.

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

E
ester.zhou 已提交
15 16
| Name        | Type                                      | Mandatory   | Description                                                        |
| ---------- | ------------------------------------------| ---- | ------------------------------------------------------------ |
E
ester.zhou 已提交
17 18
| duration   | number                                    | No   | Animation duration, in ms.<br>Default value: **1000**<br>Unit: ms<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br>- The maximum animation duration on an ArkTS widget is 1000 ms.<br>- A value less than 0 evaluates to the value **0**.<br>- Floating-point values will be rounded down to integers. For example, if the value set is 1.2, **1** will be used.|
| tempo      | number                                    | No   | Animation playback speed. A larger value indicates a higher animation playback speed.<br>The value **0** indicates that no animation is applied.<br>Default value: **1**<br>**NOTE**<br>A value less than 0 evaluates to the value **1**.|
E
ester.zhou 已提交
19
| curve      | string \| [Curve](ts-appendix-enums.md#curve) \| [ICurve](../apis/js-apis-curve.md#icurve)<sup>9+</sup> | No  | Animation curve.<br>Default value: **Curve.EaseInOut**<br>Since API version 9, this API is supported in ArkTS widgets.|
E
ester.zhou 已提交
20
| delay      | number                                    | No   | Delay of animation playback, in ms. The value **0** indicates that the playback is not delayed.<br>Default value: **0**<br>Value range: [0, +∞)<br>**NOTE**<br>- A value less than 0 evaluates to the value **0**.<br>- Floating-point values will be rounded down to integers. For example, if the value set is 1.2, **1** will be used.|
E
ester.zhou 已提交
21
| iterations | number                                    | No   | Number of times that the animation is played.<br>Default value: **1**<br>Value range: [-1, +∞)<br>**NOTE**<br>The value **-1** indicates that the animation is played for an unlimited number of times. The value **0** indicates that no animation is applied.|
E
ester.zhou 已提交
22
| 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**<br>Since API version 9, this API is supported in ArkTS widgets.<br>For details about the restrictions, see **Notes about PlayMode**.|
E
ester.zhou 已提交
23
| onFinish   | () => void                                | No   | Callback invoked when the animation playback is complete.<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br>This callback is not invoked when **iterations** is set to **-1**.|
Z
zengyawen 已提交
24

E
ester.zhou 已提交
25 26 27 28
> **Notes about PlayMode**:
> - **PlayMode.Normal** and **PlayMode.Alternate** are recommended. Under these settings, the first round of the animation is played forwards. If **PlayMode.Reverse** or **PlayMode.AlternateReverse** is used, the first round of the animation is played backwards. In this case, the animation jumps to the end state and then starts from there.
> - When using **PlayMode.Alternate** or **PlayMode.AlternateReverse**, make sure the final state of the animation is the same as the value of the state variable. In other words, make sure the last round of the animation is played forwards. When **PlayMode.Alternate** is used, **iterations** must be set to an odd number. When **PlayMode.AlternateReverse** is used, **iterations** must be set to an even number.
> - **PlayMode.Reverse** is not recommended. Under this setting, the animation jumps to the end state at the beginning, and its final state will be different from the value of the state variable.
Z
zengyawen 已提交
29 30

## Example
Z
zengyawen 已提交
31

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

  build() {
    Column() {
E
ester.zhou 已提交
44
      Button('change size')
E
ester.zhou 已提交
45
        .onClick(() => {
Z
zengyawen 已提交
46
          if (this.flag) {
E
ester.zhou 已提交
47 48
            this.widthSize = 150
            this.heightSize = 60
Z
zengyawen 已提交
49
          } else {
E
ester.zhou 已提交
50
            this.widthSize = 250
Z
zengyawen 已提交
51 52 53 54
            this.heightSize = 100
          }
          this.flag = !this.flag
        })
E
ester.zhou 已提交
55 56 57
        .margin(30)
        .width(this.widthSize)
        .height(this.heightSize)
Z
zengyawen 已提交
58
        .animation({
E
ester.zhou 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
          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,
E
ester.zhou 已提交
74 75
          iterations: -1, // The value -1 indicates that the animation is played for an unlimited number of times.
          playMode: PlayMode.Alternate
E
ester.zhou 已提交
76 77
        })
    }.width('100%').margin({ top: 20 })
Z
zengyawen 已提交
78 79 80 81
  }
}
```

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