Migration for AnimateTo Usage
During the migration of state management from V1 to V2, if you need to modify state variables before executing an animation with animateTo, you can refer to the adaptation solution in this document.
Redefining the Initial State Before Executing an Animation
V1 implementation code:
@Entry
@Component
struct Index {
@State w: number = 50; // Width.
@State h: number = 50; // Height.
@State message: string = 'Hello';
build() {
Column() {
Button('change size')
.margin(20)
.onClick(() => {
// Values are changed additionally before the animation is executed.
this.w = 100;
this.h = 100;
this.message = 'Hello World';
this.getUIContext().animateTo({
duration: 1000
}, () => {
this.w = 200;
this.h = 200;
this.message = 'Hello ArkUI';
})
})
Column() {
Text(`${this.message}`)
}
.backgroundColor('#ff17a98d')
.width(this.w)
.height(this.h)
}
}
}
Expected animation effect: The green rectangle changes from 100 x 100 to 200 x 200, and the string changes from Hello World to Hello ArkUI.

Migration from V1 to V2
@Entry
@ComponentV2
struct Index {
@Local w: number = 50; // Width.
@Local h: number = 50; // Height.
@Local message: string = 'Hello';
build() {
Column() {
Button('change size')
.margin(20)
.onClick(() => {
// Values are changed additionally before the animation is executed.
this.w = 100;
this.h = 100;
this.message = 'Hello World';
this.getUIContext().animateTo({
duration: 1000
}, () => {
this.w = 200;
this.h = 200;
this.message = 'Hello ArkUI';
})
})
Column() {
Text(`${this.message}`)
}
.backgroundColor('#ff17a98d')
.width(this.w)
.height(this.h)
}
}
}
However, due to the current incompatibility between animateTo and state management V2's update mechanism, the additional modifications before the animation do not take effect. The actual animation effect is as follows: The green rectangle transitions from 50 x 50 to 200 x 200, and the text changes from Hello to Hello ArkUI, as shown in the following figure.

Migration Solution
Migration Solution Before API Version 22
Before API version 22, you can use animateToImmediately with duration set to 0 to apply the additional modifications and then execute the original animation to achieve the expected effect.
The complete code is as follows:
@Entry
@ComponentV2
struct Index {
@Local w: number = 50; // Width.
@Local h: number = 50; // Height.
@Local message: string = 'Hello';
build() {
Column() {
Button('change size')
.margin(20)
.onClick(() => {
// Values are changed additionally before the animation is executed.
this.w = 100;
this.h = 100;
this.message = 'Hello World';
animateToImmediately({
duration: 0
}, () => {
})
this.getUIContext().animateTo({
duration: 1000
}, () => {
this.w = 200;
this.h = 200;
this.message = 'Hello ArkUI';
})
})
Column() {
Text(`${this.message}`)
}
.backgroundColor('#ff17a98d')
.width(this.w)
.height(this.h)
}
}
}
Migration Solution Since API Version 22
Since API version 22, you can use the applySync API to achieve the expected display effect.
The principle is as follows: Use the applySync API to synchronously update the state variables changes in the closure function, and then execute the original animation to achieve the expected effect.
import { UIUtils } from '@kit.ArkUI';
@Entry
@ComponentV2
struct Index {
@Local w: number = 50; // Width.
@Local h: number = 50; // Height.
@Local message: string = 'Hello';
build() {
Column() {
Button('change size')
.margin(20)
.onClick(() => {
// Values are changed additionally before the animation is executed.
UIUtils.applySync(() => {
this.w = 100;
this.h = 100;
this.message = 'Hello World';
})
this.getUIContext().animateTo({
duration: 1000
}, () => {
this.w = 200;
this.h = 200;
this.message = 'Hello ArkUI';
})
})
Column() {
Text(`${this.message}`)
}
.backgroundColor('#ff17a98d')
.width(this.w)
.height(this.h)
}
}
}
你可能感兴趣的鸿蒙文章
openharmony 鸿蒙 arkts-new-makeObserved
openharmony 鸿蒙 arkts-new-param
openharmony 鸿蒙 arkts-state-management-introduce
openharmony 鸿蒙 properly-use-state-management-to-develope
openharmony 鸿蒙 arkts-new-persistencev2
openharmony 鸿蒙 arkts-v1-v2-migration-application
openharmony 鸿蒙 arkts-v1-v2-migration-reusable
openharmony 鸿蒙 arkts-page-custom-components-lifecycle