通过AppStorage全局状态管理实现两个页面中Image组件旋转动画的同步,具体如下:
// PageOne @Entry @Component struct Index { @StorageProp('startFlag') @Watch('change') startFlag: boolean = false; @State rotateValue: number = 0; @State ifStop: number = -1; @State duration: number = 4000; change() { console.info(`startFlag: ${this.startFlag}`); if (this.startFlag) { this.duration = 4000; this.rotateValue = 360; } else { this.rotateValue = 0; } } build() { Column({ space: 30 }) { Button('点击') .onClick(() => { this.getUIContext().getRouter().pushUrl({ url: 'pages/Index2', params: { flag: '1', } }); }); // 此处'app.media.startIcon'仅作示例,请开发者自行替换,否则imageSource创建失败会导致后续无法正常执行。 Image($r('app.media.startIcon')) .width(200) .aspectRatio(1) .borderRadius(100) .rotate({ angle: this.rotateValue }) .animation({ duration: this.duration, iterations: -1, // 动画重复次数 curve: Curve.Linear, delay: 1, // 延迟时间 playMode: PlayMode.Normal }); Button('旋转').onClick(() => { this.duration = 4000; this.rotateValue = 360; AppStorage.set('startFlag', true); }); Button('停止').onClick(() => { this.duration = 0; this.rotateValue = 0; AppStorage.set('startFlag', false); }); } .justifyContent(FlexAlign.Center) .height('100%') .width('100%'); } }
// PageTwo @Entry @Component struct Index2 { @State rotateValue: number = 0; @State duration: number = 4000; @State pauseFlag: boolean = false; onPageShow(): void { const startFlag = AppStorage.get('startFlag') as boolean; console.info(`页面二:startFlag: ${startFlag}`); if (startFlag) { this.duration = 4000; this.rotateValue = 360; } else { this.duration = 0; this.rotateValue = 0; } } build() { Column({ space: 30 }) { // 此处'app.media.startIcon'仅作示例,请开发者自行替换,否则imageSource创建失败会导致后续无法正常执行。 Image($r('app.media.startIcon')) .width(200) .aspectRatio(1) .borderRadius(100) .rotate({ angle: this.rotateValue }) .animation({ duration: this.duration, iterations: -1, // 动画重复次数 curve: Curve.Linear, delay: 1, // 延迟时间 playMode: PlayMode.Normal }); Button('旋转').onClick(() => { this.duration = 4000; this.rotateValue = 360; AppStorage.set('startFlag', true); }); Button('停止').onClick(() => { this.duration = 0; this.rotateValue = 0; AppStorage.set('startFlag', false); }); Button('返回').onClick(() => { this.getUIContext().getRouter().back({ url: 'pages/Index', params: this.pauseFlag }); }); } .justifyContent(FlexAlign.Center) .height('100%') .width('100%'); } }
{
"src": [
"pages/Index",
"pages/Index2"
]
}