[uni-app] uni.createAnimation动画在APP端无效问题记录

uni.createAnimation

动画描述

实现一个以左上角为锚点,以Z轴做平面抬起及落下的动画效果

动画代码

template
					<image v-if="showHot(item.cname)" :animation="animationData" :key="item.cname"
						src="https://cdn.xxx.com/static/cert/course_fire_icon2.png" mode="">
					</image>
js部分
			initAnimation() {
				this.animation = uni.createAnimation({
					transformOrigin: "0% 100%",
					duration: 500,
					timingFunction: 'ease',
				})
				this.jumpAnimation()
			},
			jumpAnimation() {
				this.interval = setInterval(async () => {
					this.animation
						.rotateZ(-20).step()
						.rotateZ(0).step()
					this.animationData = this.animation.export();
					setTimeout(() => {
						this.animationData = null; // 清除animationData,防止二次动画执行失败
					})
				}, 2000)
			},

问题原因

以上代码在小程序端OK, 但在App端无效,面向百度开发后得知,
由于 JavaScript 的限制,Vue 不能检测以下数组
1.直接给数组的某个索引复制,
2.直接修改数组的长度,
造成APP端无效的原因就是直接赋值
this.animationData = this.animation.export();

此处就需要再 data中添加一个数组变量来实现vue对状态的监控

改进方案

template
					<image v-if="showHot(item.cname)" :animation="animaArr[0]" :key="item.cname"
						src="https://cdn.xxx.com/static/cert/course_fire_icon2.png" mode="">
					</image>

js部分

		initAnimation() {
				this.animation = uni.createAnimation({
					transformOrigin: "0% 100%",
					duration: 500,
					timingFunction: 'ease',
				})
				this.jumpAnimation()
			},
			jumpAnimation() {
				this.interval = setInterval(async () => {
					this.animation
						.rotateZ(-20).step()
						.rotateZ(0).step()
					this.animationData = this.animation;
					this.animaArr.splice(0, 1, this.animationData
						.export()); // 把动画放进 data下的空数组内, 便于vue能够监听到变化
					setTimeout(() => {
						this.animationData = null; // 清除animationData,防止二次动画执行失败
						this.animaArr = []; //定期清除 animaArr,或者说是每次动画执行后清除, 这样让vue能够再次监听到动画数组
					})
				}, 2000)
			},

改动

最主要的变动就是将
在这里插入图片描述
替换为
在这里插入图片描述

git 改进截图

在这里插入图片描述

相关推荐

  1. uni-app学习记录

    2024-03-20 06:08:06       31 阅读
  2. uni-app判断不同

    2024-03-20 06:08:06       47 阅读
  3. 学习uni-app过程中使用的css样式记录

    2024-03-20 06:08:06       34 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-20 06:08:06       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-20 06:08:06       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-20 06:08:06       87 阅读
  4. Python语言-面向对象

    2024-03-20 06:08:06       96 阅读

热门阅读

  1. C++ 类模板

    2024-03-20 06:08:06       40 阅读
  2. YOLOv8-pose自定义关键点推理封装

    2024-03-20 06:08:06       35 阅读
  3. 安卓面试题多线程41-45

    2024-03-20 06:08:06       34 阅读
  4. 【Python】复习7:面向对象编程(OOP)

    2024-03-20 06:08:06       35 阅读
  5. Qt + HTTP 线程交互类封装

    2024-03-20 06:08:06       38 阅读