动画API

本文编辑: Flyinthesky Flyinthesky浏览 7279 版权所有,严禁转载

接口说明:

动画类接口

接口 说明
wx.createAnimation(OBJECT) 创建一个动画实例animation。调用实例的方法来描述动画。最后通过动画实例的export方法导出动画数据传递给组件的animation属性。

注意: export 方法每次调用后会清掉之前的动画操作

接口用法:


【代码】

wxml

<view class="container">
  <template is="header" data="{{title: 'createAnimation'}}"/>

  <view class="page-body">
    <view class="page-body-wrapper">
      <view class="animation-element-wrapper">
        <view class="animation-element" animation="{{animation}}"></view>
      </view>
      <view class="animation-buttons" scroll-y="true">
        <button class="animation-button" bindtap="rotate">旋转</button>
        <button class="animation-button" bindtap="scale"> 缩放</button>
        <button class="animation-button" bindtap="translate">移动</button>
        <button class="animation-button" bindtap="skew">倾斜</button>
        <button class="animation-button" bindtap="rotateAndScale">旋转并缩放</button>
        <button class="animation-button" bindtap="rotateThenScale">旋转后缩放</button>
        <button class="animation-button" bindtap="all">同时展示全部</button>
        <button class="animation-button" bindtap="allInQueue">顺序展示全部</button>
        <button class="animation-button-reset" bindtap="reset">还原</button>
      </view>
    </view>
  </view>

  <template is="footer" />
</view>

js

Page({
  onReady: function () {
    this.animation = wx.createAnimation()

  },
  rotate: function () {
    this.animation.rotate(Math.random() * 720 - 360).step()
    this.setData({ animation: this.animation.export() })
  },
  scale: function () {
    this.animation.scale(Math.random() * 2).step()
    this.setData({ animation: this.animation.export() })
  },
  translate: function () {
    this.animation.translate(Math.random() * 100 - 50, Math.random() * 100 - 50).step()
    this.setData({ animation: this.animation.export() })
  },
  skew: function () {
    this.animation.skew(Math.random() * 90, Math.random() * 90).step()
    this.setData({ animation: this.animation.export() })
  },
  rotateAndScale: function () {
    this.animation.rotate(Math.random() * 720 - 360)
        .scale(Math.random() * 2)
        .step()
    this.setData({ animation: this.animation.export() })
  },
  rotateThenScale: function () {
    this.animation.rotate(Math.random() * 720 - 360).step()
        .scale(Math.random() * 2).step()
    this.setData({ animation: this.animation.export() })
  },
  all: function () {
    this.animation.rotate(Math.random() * 720 - 360)
        .scale(Math.random() * 2)
        .translate(Math.random() * 100 - 50, Math.random() * 100 - 50)
        .skew(Math.random() * 90, Math.random() * 90)
        .step()
    this.setData({ animation: this.animation.export() })
  },
  allInQueue: function () {
    this.animation.rotate(Math.random() * 720 - 360).step()
        .scale(Math.random() * 2).step()
        .translate(Math.random() * 100 - 50, Math.random() * 100 - 50).step()
        .skew(Math.random() * 90, Math.random() * 90).step()
    this.setData({ animation: this.animation.export() })
  },
  reset: function () {
    this.animation.rotate(0, 0)
                  .scale(1)
                  .translate(0, 0)
                  .skew(0, 0)
                  .step({ duration: 0 })
    this.setData({ animation: this.animation.export() })
  }
})

wxss

.page-body-wrapper {
  flex-grow: 1;
}
.animation-element-wrapper {
  display: block;
  margin-bottom: 100rpx;
}
.animation-element {
  width: 200rpx;
  height: 200rpx;
  background-color: #1AAD19;
}
.animation-buttons {
  padding: 50rpx 50rpx 10rpx;
  border-top: 1px solid #ccc;
  display: flex;
  flex-grow: 1;
  overflow-y: scroll;
  flex-direction: row;
  flex-wrap: wrap;
  width: 100%;
  height: 400rpx;
  box-sizing: border-box;
}
.animation-button {
  width: 290rpx;
  margin: 20rpx auto; 
}
.animation-button-reset {
  width: 610rpx;
  margin: 20rpx auto; 
}

主要方法:

wx.createAnimation(OBJECT):创建一个动画实例

OBJECT参数说明:

参数 类型 必填 说明
duration Integer 动画持续时间,单位ms,默认值 400
timingFunction String 定义动画的效果,默认值”linear”,有效值:”linear”,”ease”,”ease-in”,”ease-in-out”,”ease-out”,”step-start”,”step-end”
delay Integer 动画延迟时间,单位 ms,默认值 0
transformOrigin String 设置transform-origin,默认为”50% 50% 0”

示例代码:

var animation = wx.createAnimation({
  transformOrigin: "50% 50%",
  duration: 1000,
  timingFunction: "ease",
  delay: 0
})

animation
动画实例可以调用以下方法来描述动画,调用结束后会返回自身,支持链式调用的写法。

样式:

方法 参数 说明
opacity value 透明度,参数范围 0~1
backgroundColor color 颜色值
width length 长度值,如果传入 Number 则默认使用 px,可传入其他自定义单位的长度值
height length 长度值,如果传入 Number 则默认使用 px,可传入其他自定义单位的长度值
top length 长度值,如果传入 Number 则默认使用 px,可传入其他自定义单位的长度值
left length 长度值,如果传入 Number 则默认使用 px,可传入其他自定义单位的长度值
bottom length 长度值,如果传入 Number 则默认使用 px,可传入其他自定义单位的长度值
right length 长度值,如果传入 Number 则默认使用 px,可传入其他自定义单位的长度值

旋转:

方法 参数 说明
rotate deg deg的范围-180~180,从原点顺时针旋转一个deg角度
rotateX deg deg的范围-180~180,在X轴旋转一个deg角度
rotateY deg deg的范围-180~180,在Y轴旋转一个deg角度
rotateZ deg deg的范围-180~180,在Z轴旋转一个deg角度
rotate3d (x,y,z,deg) 同transform-function rotate3d

缩放:

方法 参数 说明
scale sx,[sy] 一个参数时,表示在X轴、Y轴同时缩放sx倍数;两个参数时表示在X轴缩放sx倍数,在Y轴缩放sy倍数
scaleX sx 在X轴缩放sx倍数
scaleY sy 在Y轴缩放sy倍数
scaleZ sz 在Z轴缩放sy倍数
scale3d (sx,sy,sz) 在X轴缩放sx倍数,在Y轴缩放sy倍数,在Z轴缩放sz倍数

偏移:

方法 参数 说明
translate tx,[ty] 一个参数时,表示在X轴偏移tx,单位px;两个参数时,表示在X轴偏移tx,在Y轴偏移ty,单位px。
translateX tx 在X轴偏移tx,单位px
translateY ty 在Y轴偏移tx,单位px
translateZ tz 在Z轴偏移tx,单位px
translate3d (tx,ty,tz) 在X轴偏移tx,在Y轴偏移ty,在Z轴偏移tz,单位px

倾斜:

方法 参数 说明
skew ax,[ay] 参数范围-180~180;一个参数时,Y轴坐标不变,X轴坐标延顺时针倾斜ax度;两个参数时,分别在X轴倾斜ax度,在Y轴倾斜ay度
skewX ax 参数范围-180~180;Y轴坐标不变,X轴坐标延顺时针倾斜ax度
skewY ay 参数范围-180~180;X轴坐标不变,Y轴坐标延顺时针倾斜ay度

矩阵变形:

方法 参数 说明
matrix (a,b,c,d,tx,ty) 同transform-function matrix
matrix3d 同transform-function matrix3d

动画队列
调用动画操作方法后要调用 step() 来表示一组动画完成,可以在一组动画中调用任意多个动画方法,一组动画中的所有动画会同时开始,一组动画完成后才会进行下一组动画。step 可以传入一个跟 wx.createAnimation()一样的配置参数用于指定当前组动画的配置。
示例代码:
wxml

<view animation="{{animationData}}" style="background:red;height:100rpx;width:100rpx"></view>

js

Page({
  data: {
    animationData: {}
  },
  onShow: function(){
    var animation = wx.createAnimation({
      duration: 1000,
        timingFunction: 'ease',
    })

    this.animation = animation

    animation.scale(2,2).rotate(45).step()

    this.setData({
      animationData:animation.export()
    })

    setTimeout(function() {
      animation.translate(30).step()
      this.setData({
        animationData:animation.export()
      })
    }.bind(this), 1000)
  },
  rotateAndScale: function () {
    // 旋转同时放大
    this.animation.rotate(45).scale(2, 2).step()
    this.setData({
      animationData: this.animation.export()
    })
  },
  rotateThenScale: function () {
    // 先旋转后放大
    this.animation.rotate(45).step()
    this.animation.scale(2, 2).step()
    this.setData({
      animationData: this.animation.export()
    })
  },
  rotateAndScaleThenTranslate: function () {
    // 先旋转同时放大,然后平移
    this.animation.rotate(45).scale(2, 2).step()
    this.animation.translate(100, 100).step({ duration: 1000 })
    this.setData({
      animationData: this.animation.export()
    })
  }
})

bug & tip
1、bug: iOS/Android 6.3.30 通过 step() 分隔动画,只有第一步动画能生效。

如有技术问题或对本文有反馈,请加入QQ群:
微信小程序实战5营: 微信小程序Club实战5营