HarmonyOS 鸿蒙Next @Watch 事件中动画无法启动
HarmonyOS 鸿蒙Next @Watch 事件中动画无法启动 发现@Watch 事件中ImageAnimator动画生命周期都执行了,但是动画不动(手动点击可以动),是否有什么底层控制逻辑?
代码片段如下:
@Prop [@Watch](/user/Watch)('externalAutoPlayChannel')
autoPlayChannel: boolean = false;
externalAutoPlayChannel() {
this.playSound();
}
ImageAnimator()
.images(this.getImageFrames())
.duration(800)
.state(this.animationStatus)
.fillMode(FillMode.Backwards)
.reverse(false)
.fixedSize(true)
.iterations(-1)
.width(this.componentSize)
.height(this.componentSize)
.onStart(() => {
AppLogApi.debug(this.tag, `ImageAnimator onStart()`);
})
.onPause(() => {
AppLogApi.debug(this.tag, `ImageAnimator onPause()`);
})
.onRepeat(() => {
AppLogApi.debug(this.tag, `ImageAnimator onRepeat()`);
})
.onCancel(() => {
AppLogApi.debug(this.tag, `ImageAnimator onCancel()`);
this.animationStatus = AnimationStatus.Stopped
})
.onFinish(() => {
AppLogApi.debug(this.tag, `ImageAnimator onFinish()`);
this.animationStatus = AnimationStatus.Stopped
})
.onClick((_event) => {
this.playSound();
})
更多关于HarmonyOS 鸿蒙Next @Watch 事件中动画无法启动的实战教程也可以访问 https://www.itying.com/category-93-b0.html
2 回复
根据楼主提供代码片段,我这边试了一下是watch是正常生效的,你看看demo
@Component
struct ImageAnimatorComponent {
@State state: AnimationStatus = AnimationStatus.Initial
@State reverse: boolean = false
@State iterations: number = 1
@State @Watch('externalAutoPlayChannel') autoPlayChannel: boolean = false;
externalAutoPlayChannel() {
if(this.autoPlayChannel === true){
this.state = AnimationStatus.Running
}
}
build() {
Column({ space: 10 }) {
ImageAnimator()
.images([
{
src: $r('app.media.video_create_gif_preview1')
},
{
src: $r('app.media.video_create_gif_preview2')
},
{
src: $r('app.media.video_create_gif_preview3')
},
{
src: $r('app.media.video_create_gif_preview4')
}
])
.duration(2000)
.state(this.state).reverse(this.reverse)
.fillMode(FillMode.None).iterations(this.iterations).width(340).height(240)
.margin({ top: 100 })
.onStart(() => {
console.info('Start')
})
.onPause(() => {
console.info('Pause')
})
.onRepeat(() => {
console.info('Repeat')
})
.onCancel(() => {
console.info('Cancel')
})
.onFinish(() => {
console.info('Finish')
this.state = AnimationStatus.Stopped
})
Button('change autoPlayChannel').width(100).padding(5).onClick(() => {
this.autoPlayChannel = !this.autoPlayChannel
}).margin(5)
Row() {
Button('start').width(100).padding(5).onClick(() => {
this.state = AnimationStatus.Running
}).margin(5)
Button('pause').width(100).padding(5).onClick(() => {
this.state = AnimationStatus.Paused // 显示当前帧图片
}).margin(5)
Button('stop').width(100).padding(5).onClick(() => {
this.state = AnimationStatus.Stopped // 显示动画的起始帧图片
}).margin(5)
}
Row() {
Button('reverse').width(100).padding(5).onClick(() => {
this.reverse = !this.reverse
}).margin(5)
Button('once').width(100).padding(5).onClick(() => {
this.iterations = 1
}).margin(5)
Button('infinite').width(100).padding(5).onClick(() => {
this.iterations = -1 // 无限循环播放
}).margin(5)
}
}.width('100%').height('100%')
}
}
更多关于HarmonyOS 鸿蒙Next @Watch 事件中动画无法启动的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html