ts-explicit-animation.md 5.7 KB
Newer Older
Z
zengyawen 已提交
1
# Explicit Animation
Z
zengyawen 已提交
2

E
esterzhou 已提交
3
You can create explicit animation with your custom settings.
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.
E
ester.zhou 已提交
8 9 10 11
>
> The functionality of this module depends on UI context. This means that the APIs of this module cannot be used where the UI context is unclear. For details, see [UIContext](../apis/js-apis-arkui-UIContext.md#uicontext).
>
> Since API version 10, you can use the [animateTo](../apis/js-apis-arkui-UIContext.md#animateto) API in [UIContext](../apis/js-apis-arkui-UIContext.md#uicontext) to obtain the UI context.
Z
zengyawen 已提交
12

E
ester.zhou 已提交
13
animateTo(value: AnimateParam, event: () => void): void
Z
zengyawen 已提交
14

E
ester.zhou 已提交
15 16
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
17 18 19 20
| Name            | Type       |       Mandatory    |        Description       |
| ---------------- | ------------ | -------------------- | -------------------- |
| value | [AnimateParam](#animateparam)| Yes| Animation settings.|
| event | () => void | Yes| Closure function that displays the dynamic effect. The system automatically inserts the transition animation if the status changes in the closure function.|
Z
zengyawen 已提交
21

E
ester.zhou 已提交
22
## AnimateParam
Z
zengyawen 已提交
23

E
ester.zhou 已提交
24 25
| Name| Type| Description|
| -------- | -------- | -------- |
E
ester.zhou 已提交
26 27
| duration | number | Animation duration, in ms.<br>Default value: **1000**<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. If the set value exceeds the limit, the value **1000** will be used.<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 | Animation playback speed. A larger value indicates faster animation playback, and a smaller value indicates slower animation playback. The value **0** means that there is no animation.<br>Default value: **1.0**<br>**NOTE**<br>A value less than 0 evaluates to the value **1**.|
E
ester.zhou 已提交
28
| curve | [Curve](ts-appendix-enums.md#curve) \| [ICurve](../apis/js-apis-curve.md#icurve) \| string | Animation curve.<br>Default value: **Curve.Linear**<br>Since API version 9, this API is supported in ArkTS widgets.|
E
ester.zhou 已提交
29
| delay | number | Delay of animation playback, in ms. By default, the playback is not delayed.<br>Default value: **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 已提交
30
| iterations | number | Number of times that the animation is played. By default, the animation is played once. The value **-1** indicates that the animation is played for an unlimited number of times.<br>Default value: **1**|
E
ester.zhou 已提交
31
| playMode | [PlayMode](ts-appendix-enums.md#playmode) | 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 已提交
32
| onFinish   | () =&gt; void   | Callback invoked when the animation playback is complete.<br>Since API version 9, this API is supported in ArkTS widgets.|
E
ester.zhou 已提交
33

E
ester.zhou 已提交
34 35 36 37
> **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 已提交
38

Z
zengyawen 已提交
39 40
## Example

Mr-YX's avatar
Mr-YX 已提交
41
```ts
E
esterzhou 已提交
42
// xxx.ets
Z
zengyawen 已提交
43 44 45
@Entry
@Component
struct AnimateToExample {
E
ester.zhou 已提交
46
  @State widthSize: number = 250
Z
zengyawen 已提交
47
  @State heightSize: number = 100
E
ester.zhou 已提交
48
  @State rotateAngle: number = 0
Z
zengyawen 已提交
49 50 51 52
  private flag: boolean = true

  build() {
    Column() {
E
ester.zhou 已提交
53
      Button('change size')
Z
zengyawen 已提交
54 55
        .width(this.widthSize)
        .height(this.heightSize)
E
ester.zhou 已提交
56 57
        .margin(30)
        .onClick(() => {
Z
zengyawen 已提交
58 59
          if (this.flag) {
            animateTo({
E
ester.zhou 已提交
60 61 62 63
              duration: 2000,
              curve: Curve.EaseOut,
              iterations: 3,
              playMode: PlayMode.Normal,
Z
zengyawen 已提交
64 65 66 67
              onFinish: () => {
                console.info('play end')
              }
            }, () => {
E
ester.zhou 已提交
68 69
              this.widthSize = 150
              this.heightSize = 60
Z
zengyawen 已提交
70 71
            })
          } else {
E
ester.zhou 已提交
72 73
            animateTo({}, () => {
              this.widthSize = 250
Z
zengyawen 已提交
74 75 76 77 78
              this.heightSize = 100
            })
          }
          this.flag = !this.flag
        })
E
ester.zhou 已提交
79 80 81 82 83 84 85 86 87
      Button('change rotate angle')
        .margin(50)
        .rotate({ x: 0, y: 0, z: 1, angle: this.rotateAngle })
        .onClick(() => {
          animateTo({
            duration: 1200,
            curve: Curve.Friction,
            delay: 500,
            iterations: -1, // The value -1 indicates that the animation is played for an unlimited number of times.
E
ester.zhou 已提交
88
            playMode: PlayMode.Alternate,
E
ester.zhou 已提交
89 90 91 92 93 94 95
            onFinish: () => {
              console.info('play end')
            }
          }, () => {
            this.rotateAngle = 90
          })
        })
Z
zengyawen 已提交
96 97 98 99 100
    }.width('100%').margin({ top: 5 })
  }
}
```

E
ester.zhou 已提交
101
![animation1](figures/animation1.gif)