1e41f4b71Sopenharmony_ci# Keyframe Animation (keyframeAnimateTo) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe [UIContext](../js-apis-arkui-UIContext.md#uicontext) provides the **keyframeAnimateTo** API to specify several keyframes to implement segment-based animation. In an animation that involves width and height changes, as in a property animation, a component's content (such as text, [canvas](ts-components-canvas-canvas.md) content, and [linear gradient](ts-universal-attributes-gradient-color.md)) is changed straight to the final state. To enable the content to change with the width and height during the animation process, you can use the [renderFit](ts-universal-attributes-renderfit.md) attribute. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> This feature is supported since API version 11. Updates will be marked with a superscript to indicate their earliest API version. 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> This API is a member function of the [UIContext](../js-apis-arkui-UIContext.md#uicontext) class and needs to be called through a **UIContext** instance. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_cikeyframeAnimateTo(param: KeyframeAnimateParam, keyframes: Array<KeyframeState>): void 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ciSets the keyframe animation. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci**Parameters** 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 22e41f4b71Sopenharmony_ci| ------------ | ---------------------------------------------------- | ------- | ---------------------------- | 23e41f4b71Sopenharmony_ci| param | [KeyframeAnimateParam](#keyframeanimateparam) | Yes | Global parameters of the keyframe animation. | 24e41f4b71Sopenharmony_ci| keyframes | Array<[KeyframeState](#keyframestate)> | Yes | Array of keyframes. | 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci## KeyframeAnimateParam 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 31e41f4b71Sopenharmony_ci| ---------- | ---------- | ------- | ------------------------------------- | 32e41f4b71Sopenharmony_ci| delay | number | No | Delay of animation playback, in ms.<br>Default value: **0**, indicating that the playback is not delayed.<br>**NOTE**<br>A value greater than 0 means to begin the animation after the specified amount of time has elapsed.<br>A value less than 0 means to begin the animation in advance. If the absolute value of **delay** is less than the actual animation duration, the animation starts its first frame from the state at the absolute value. If the absolute value of **delay** is greater than or equal to the actual animation duration, the animation starts its first frame from the end state. The actual animation duration is equal to the duration of a single animation multiplied by the number of animation playback times.| 33e41f4b71Sopenharmony_ci| iterations | number | No | 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. The value **0** indicates that there is no animation.<br>Default value: **1**<br>Value range: [-1, +∞)| 34e41f4b71Sopenharmony_ci| onFinish | () => void | No | Callback invoked when the animation playback is complete. This API is called after the keyframe animation has played for the specified number of times.| 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci## KeyframeState 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 41e41f4b71Sopenharmony_ci| ---------- | ------------------------------------ | ------- | ---------------------------------------- | 42e41f4b71Sopenharmony_ci| duration | number | Yes | Duration of the keyframe animation, in ms.<br>Value range: [0, +∞)<br>**NOTE**<br>- If this parameter is set to a value less than 0, the value **0** is used.<br>- Floating-point values will be rounded down to integers. For example, if the value set is 1.2, **1** will be used.| 43e41f4b71Sopenharmony_ci| curve | [Curve](ts-appendix-enums.md#curve)\| string \| [ICurve](../js-apis-curve.md#icurve9) | No | Animation curve used by the keyframe.<br>Default value: **Curve.EaseInOut**<br>**NOTE**<br>Because the[springMotion](../js-apis-curve.md#curvesspringmotion9), [responsiveSpringMotion](../js-apis-curve.md#curvesresponsivespringmotion9), and [interpolatingSpring](../js-apis-curve.md#curvesinterpolatingspring10) curves do not have effective duration settings, they are not supported.| 44e41f4b71Sopenharmony_ci| event | () => void | Yes | Closure function of the state at the time of the keyframe, that is, the state to be reached at the time of the keyframe.| 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci## Example 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci```ts 49e41f4b71Sopenharmony_ci// xxx.ets 50e41f4b71Sopenharmony_ciimport { UIContext } from '@kit.ArkUI'; 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci@Entry 53e41f4b71Sopenharmony_ci@Component 54e41f4b71Sopenharmony_cistruct KeyframeDemo { 55e41f4b71Sopenharmony_ci @State myScale: number = 1.0; 56e41f4b71Sopenharmony_ci uiContext: UIContext | undefined = undefined; 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci aboutToAppear() { 59e41f4b71Sopenharmony_ci this.uiContext = this.getUIContext?.(); 60e41f4b71Sopenharmony_ci } 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci build() { 63e41f4b71Sopenharmony_ci Column() { 64e41f4b71Sopenharmony_ci Circle() 65e41f4b71Sopenharmony_ci .width(100) 66e41f4b71Sopenharmony_ci .height(100) 67e41f4b71Sopenharmony_ci .fill("#46B1E3") 68e41f4b71Sopenharmony_ci .margin(100) 69e41f4b71Sopenharmony_ci .scale({ x: this.myScale, y: this.myScale }) 70e41f4b71Sopenharmony_ci .onClick(() => { 71e41f4b71Sopenharmony_ci if (!this.uiContext) { 72e41f4b71Sopenharmony_ci console.info("no uiContext, keyframe failed"); 73e41f4b71Sopenharmony_ci return; 74e41f4b71Sopenharmony_ci } 75e41f4b71Sopenharmony_ci this.myScale = 1; 76e41f4b71Sopenharmony_ci // Set the keyframe animation to play three times. 77e41f4b71Sopenharmony_ci this.uiContext.keyframeAnimateTo({ iterations: 3 }, [ 78e41f4b71Sopenharmony_ci { 79e41f4b71Sopenharmony_ci // The first keyframe animation lasts for 800 ms, during which the scale attribute changes from 1 to 1.5. 80e41f4b71Sopenharmony_ci duration: 800, 81e41f4b71Sopenharmony_ci event: () => { 82e41f4b71Sopenharmony_ci this.myScale = 1.5; 83e41f4b71Sopenharmony_ci } 84e41f4b71Sopenharmony_ci }, 85e41f4b71Sopenharmony_ci { 86e41f4b71Sopenharmony_ci // The second keyframe animation lasts for 500 ms, during which the scale attribute changes from 1.5 to 1. 87e41f4b71Sopenharmony_ci duration: 500, 88e41f4b71Sopenharmony_ci event: () => { 89e41f4b71Sopenharmony_ci this.myScale = 1; 90e41f4b71Sopenharmony_ci } 91e41f4b71Sopenharmony_ci } 92e41f4b71Sopenharmony_ci ]); 93e41f4b71Sopenharmony_ci }) 94e41f4b71Sopenharmony_ci }.width('100%').margin({ top: 5 }) 95e41f4b71Sopenharmony_ci } 96e41f4b71Sopenharmony_ci} 97e41f4b71Sopenharmony_ci``` 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci 100