最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

微信小程序 Animation实现图片旋转动画示例

来源:懂视网 责编:小采 时间:2020-11-27 22:09:22
文档

微信小程序 Animation实现图片旋转动画示例

微信小程序 Animation实现图片旋转动画示例:最近小程序中有一个图片旋转的需求,最初是想着通过切换多张图片达到旋转的效果,后来发现微信小程序带有动画api,然后就改由image+Animation来实现。 首先在wxml中定义image <image class=bth_image2 mode=aspectFit ani
推荐度:
导读微信小程序 Animation实现图片旋转动画示例:最近小程序中有一个图片旋转的需求,最初是想着通过切换多张图片达到旋转的效果,后来发现微信小程序带有动画api,然后就改由image+Animation来实现。 首先在wxml中定义image <image class=bth_image2 mode=aspectFit ani

最近小程序中有一个图片旋转的需求,最初是想着通过切换多张图片达到旋转的效果,后来发现微信小程序带有动画api,然后就改由image+Animation来实现。

首先在wxml中定义image

<image class="bth_image2" mode="aspectFit" animation="{{animation}}" src='../../images/***.png'></image>

注意其中的animation属性,image就由它来实现动画。

而{{animation}}我们在js的data中定义

data: {
 animation: ''
},

改变animation的值(官网提供角度范围是-180~180,但是我发现角度越大会一直旋转)

 onShow: function() {
 console.log('index---------onShow()')
 this.animation = wx.createAnimation({
 duration: 1400,
 timingFunction: 'linear', // "linear","ease","ease-in","ease-in-out","ease-out","step-start","step-end"
 delay: 0,
 transformOrigin: '50% 50% 0',
 success: function(res) {
 console.log("res")
 }
 })
 },
 rotateAni: function (n) {
 console.log("rotate=="+n)
 this.animation.rotate(180*(n)).step()
 this.setData({
 animation: this.animation.export()
 })
 },

相关代码

var _animation;
var _animationIndex
const _ANIMATION_TIME = 500;
pages {
...
 onShow: function () {
 _animation = wx.createAnimation({
 duration: _ANIMATION_TIME,
 timingFunction: 'linear', // "linear","ease","ease-in","ease-in-out","ease-out","step-start","step-end"
 delay: 0,
 transformOrigin: '50% 50% 0'
})
},

/**
 * 实现image旋转动画,每次旋转 120*n度
 */
 rotateAni: function (n) {
 _animation.rotate(120 * (n)).step()
this.setData({
 animation: _animation.export()
})
},

/**
 * 开始旋转
 */
 startAnimationInterval: function () {
var that = this;
 that.rotateAni(++_loadImagePathIndex); // 进行一次旋转
 _animationIntervalId = setInterval(function () {
 that.rotateAni(++_loadImagePathIndex);
}, _ANIMATION_TIME); // 没间隔_ANIMATION_TIME进行一次旋转
},

/**
 * 停止旋转
 */
 stopAnimationInterval: function () {
if (_animationIntervalId> 0) {
 clearInterval(_animationIntervalId);
 _animationIntervalId = 0;
}
},
}

微信自带的Animation可以实现一次动画,然后可以通过setInterval来达到不断旋转的目的,在使用时,控制startAnimationInterval和stopAnimationInterval即可。

注意:

这里为什么不直接给_animation.rotate(120 * (n)).step()设置一个足够大的值,原因有两点:

1、我们需要便利的控制开始和停止,

2、animation在小程序进入后台后,会持续运行,占用手机内存和cpu,而小程序依赖于微信,在iphone上会导致微信被终止运行

在使用animation时,会发现有时候出现旋转速度很快或者反向旋转再正向旋转的清空,这都是由于rotate的值设置有问题。

1、rotate的值应该是上一次结束时的值,
2、如果设置了全局变量,记得在oncreate时初始化,不然第二次打开同一页面会有问题。

文档

微信小程序 Animation实现图片旋转动画示例

微信小程序 Animation实现图片旋转动画示例:最近小程序中有一个图片旋转的需求,最初是想着通过切换多张图片达到旋转的效果,后来发现微信小程序带有动画api,然后就改由image+Animation来实现。 首先在wxml中定义image <image class=bth_image2 mode=aspectFit ani
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top