ts-explicit-animation.md 3.6 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.
Z
zengyawen 已提交
8

E
esterzhou 已提交
9 10 11

| API                                                    | Description                                                    |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
E
ester.zhou 已提交
12
| animateTo(value: [AnimateParam](#animateparam), event: ()=&gt; void) : void | Provides a transition animation when the status changes due to the closure code.<br>**event** specifies the closure function that displays the dynamic effect. The system automatically inserts the transition animation if the status changes in the closure function.|
Z
zengyawen 已提交
13 14


E
ester.zhou 已提交
15
## AnimateParam
Z
zengyawen 已提交
16

E
ester.zhou 已提交
17 18 19 20 21 22 23 24 25 26
| Name| Type| Description|
| -------- | -------- | -------- |
| duration | number | Animation duration, in ms.<br>Default value: **1000**|
| 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**|
| curve | Curve \| Curves | Animation curve.<br>Default value: **Curve.Linear**|
| delay | number | Delay of animation playback, in ms. By default, the playback is not delayed.<br>Default value: **0**|
| 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**|
| 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**|
| onFinish   | () =&gt; void   | Callback invoked when the animation playback is complete.|

Z
zengyawen 已提交
27 28


Z
zengyawen 已提交
29 30
## Example

Mr-YX's avatar
Mr-YX 已提交
31
```ts
E
esterzhou 已提交
32
// xxx.ets
Z
zengyawen 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46
@Entry
@Component
struct AnimateToExample {
  @State widthSize: number = 200
  @State heightSize: number = 100
  private flag: boolean = true

  build() {
    Column() {
      Button('click me')
        .width(this.widthSize)
        .height(this.heightSize)
        .backgroundColor(0x317aff)
        .onClick((event: ClickEvent) => {
E
esterzhou 已提交
47
          // Animation configuration for the width and height attributes of the <Button> component
Z
zengyawen 已提交
48 49
          if (this.flag) {
            animateTo({
E
esterzhou 已提交
50 51 52 53 54 55
              duration: 1000, // Animation duration
              tempo: 0.5, // Playback speed
              curve: Curve.EaseInOut, // Animation curve
              delay: 200, // Animation delay
              iterations: 1, // Number of playback times
              playMode: PlayMode.Normal // Animation playback mode
Z
zengyawen 已提交
56 57 58 59 60 61 62 63 64
              onFinish: () => {
                console.info('play end')
              }
            }, () => {
              this.widthSize = 100
              this.heightSize = 50
            })
          } else {
            animateTo({
E
esterzhou 已提交
65 66 67 68 69
              duration: 200, // Animation duration
              curve: Curve.Ease, // Animated curve
              delay: 200, // Animation delay
              iterations: 1, // Number of playback times
              playMode: PlayMode.Normal // Animation playback mode
Z
zengyawen 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
              onFinish: () => {
                console.info('play end')
              }
            }, () => {
              this.widthSize = 200
              this.heightSize = 100
            })
          }
          this.flag = !this.flag
        })
    }.width('100%').margin({ top: 5 })
  }
}
```

E
esterzhou 已提交
85
![en-us_image_0000001256978345](figures/en-us_image_0000001256978345.gif)