问题现象
当组件在动画执行过程中,对组件当前显示位置进行点击操作,无法触发onClick事件,唯有当动画结束、组件停止在最终位置后,该组件的点击操作才可正常响应。如何在动画执行过程中使组件能正确响应onClick事件?
@Entry
@Component
struct Problem {
@State title: string = '点击更新时间参数:';
@State x: number = 0;
startAnimation() {
this.getUIContext().animateTo({ duration: 5000, playMode: PlayMode.Normal }, () => {
this.x = 100;
});
}
build() {
Stack() {
Text(this.title)
.fontSize(18)
.position({ x: this.x, y: 40 })
.animation({
duration: 5000,
curve: Curve.Linear,
playMode: PlayMode.Normal
})
.onClick(() => {
this.title = '点击更新时间参数:' + new Date().getTime();
})
.onAppear(() => {
this.startAnimation();
});
}
.height(100)
.width('100%');
}
}
背景知识
@ohos.animator(动画):提供组件动画效果,包括定义动画、启动动画和以相反的顺序播放动画等。
显式动画(animateTo):提供全局animateTo显式动画接口来指定由于闭包代码导致的状态变化插入过渡动效。
解决方案
由于显式动画的特性,目标对象在动画执行期间所处的位置仅在动画结束时被识别和记录。当用户在动画播放过程中尝试点击该对象时,点击事件无法被正确识别和响应。
为了解决这一问题,可以采用帧动画的方式来实现,帧动画通过逐帧定义对象的状态,系统会在每一帧更新对象的位置和状态,并据此响应用户交互事件。完整示例代码参考如下:
import { AnimatorResult } from '@kit.ArkUI';
@Entry
@Component
struct SolutionOne {
@State title: string = '点击更新时间参数:';
@State x: number = 0;
private backAnimator: AnimatorResult = this.getUIContext().createAnimator({
duration: 5000,
easing: 'ease',
delay: 0,
fill: 'forwards',
direction: 'normal',
iterations: -1,
begin: 0,
end: 100
});
build() {
Stack() {
Text(this.title)
.fontSize(18)
.position({ x: this.x, y: 40 })
.onClick(() => {
this.title = '点击更新时间参数:' + new Date().getTime();
})
.onAppear(() => {
this.backAnimator.play();
this.backAnimator.onFrame = (value: number) => {
this.x = value;
};
});
}
.height(100)
.width('100%');
}
}
https://coub.com/view/x0xla7q1ai
https://coub.com/view/g1czsl0enm
https://coub.com/view/b08cx7zoe3
https://coub.com/view/bxn2yqwexg
https://coub.com/view/m9qeccobgj
https://coub.com/view/wdvoecbi4r
https://coub.com/view/x81cx0az2t
https://coub.com/view/0slfmtknp8
https://coub.com/view/2q5tc8yg66
https://coub.com/view/dt2oigt51x
https://coub.com/view/nrlgm47wti
https://coub.com/view/sp90u3ia89
https://coub.com/view/o78qotj8bn
https://coub.com/view/1duw99ytde
https://coub.com/view/wjcskrvoln
https://coub.com/view/8obmr0ccrs
https://coub.com/view/sw5f17cga5
https://coub.com/view/s1q3otk2wu
https://coub.com/view/83z3yr5cy4
https://coub.com/view/brgme2h0fi
https://coub.com/view/jkh8n5pj1b
https://coub.com/view/vode602lzv
https://coub.com/view/ue3i8rhiey
https://coub.com/view/76wrcka2m1
https://coub.com/view/casxikde15
https://coub.com/view/lb2qd32pue
https://coub.com/view/vqz0ngc7lp
https://coub.com/view/5x8wrqa4sk
https://coub.com/view/s22g4m3o4b
https://coub.com/view/a7oblam77e
https://coub.com/view/nkne5vbzgv
https://coub.com/view/92pdw2n47o
https://coub.com/view/wi0fyfnyj6
https://coub.com/view/lrcrn6sklh
https://coub.com/view/ty636zpx1j
https://coub.com/view/1p4s8n800f
https://coub.com/view/2ewu7lqpse
https://coub.com/view/ees1i4x52y
https://coub.com/view/esuw3humxk
https://coub.com/view/ttof5yluqt
https://coub.com/view/3788oh5mhd
https://coub.com/view/zprnnzdm9y
https://coub.com/view/1758nsm1bp
https://coub.com/view/85robtp98p
https://coub.com/view/pwhyjoe2qk
https://coub.com/view/fk76ykwiph
https://coub.com/view/2qpw8qa4v6
https://coub.com/view/qmich3xb6k
https://coub.com/view/0fpkrydd0k
https://coub.com/view/bvzgv7jbfc
https://coub.com/view/nvjqk9hmiz
https://coub.com/view/0le4asq8nh
https://coub.com/view/rkrsrwn90s
https://coub.com/view/f2uu2z8u1s
https://coub.com/view/715y2xacjj
https://coub.com/view/6v5dzfjzbn
https://coub.com/view/wxk5a8mxbn
https://coub.com/view/o05ggpa1bk
https://coub.com/view/v04o0y81aa
https://coub.com/view/e0fsub9p1f
https://coub.com/view/tkv88d5t0f
https://coub.com/view/xz6i7n2qay
https://coub.com/view/d211sbq00l
https://coub.com/view/zypkg0eyho
https://coub.com/view/8r0gx25nyg
https://coub.com/view/63tzntskz6
https://coub.com/view/3jsg85l39s
https://coub.com/view/hr4idq4466
https://coub.com/view/h4drvxmmmt
https://coub.com/view/schlv1biel
https://coub.com/view/f4uuetk2yf
https://coub.com/view/m2z1rd0e4k
https://coub.com/view/s94flprsy0
https://coub.com/view/smusvxthpl
https://coub.com/view/p0qj7yhx1p
https://coub.com/view/5ahyecg1hb
https://coub.com/view/hgbpl5bpg1
https://coub.com/view/0xmq3ond07
https://coub.com/view/zf7r0w2yyb
https://coub.com/view/bd0elp9k2p
0 个评论
要回复文章请先登录或注册