原生小程序开发 API|界面动画 API 汇总(二)

ty.createAnimation

创建一个动画实例 animation。调用实例的方法来描述动画。最后通过动画实例的 export 方法导出动画数据传递给组件的 animation 属性。

参数

Object object
属性类型默认值必填说明
durationnumber400动画持续时间,单位 ms
timingFunctionstring'linear'动画的效果
delaynumber0动画延迟时间,单位 ms
transformOriginstring'50% 50% 0'
timingFunction 的合法值
说明最低版本
'linear'动画从头到尾的速度是相同的
'ease'动画以低速开始,然后加快,在结束前变慢
'ease-in'动画以低速开始
'ease-in-out'动画以低速开始和结束
'ease-out'动画以低速结束
'step-start'动画第一帧就跳至结束状态直到结束
'step-end'动画一直保持开始状态,最后一帧跳到结束状态

返回值 Animation

示例代码

<view class="container">
  <view class="page-body">
    <view class="page-section">
      <view class="animation-element-wrapper">
        <view class="animation-element" animation="{{animation}}"></view>
      </view>
      <view class="animation-buttons" scroll-y="true">
        <button class="animation-button" bind:tap="rotate">旋转</button>
        <button class="animation-button" bind:tap="scale">缩放</button>
        <button class="animation-button" bind:tap="translate">移动</button>
        <button class="animation-button" bind:tap="skew">倾斜</button>
        <button class="animation-button" bind:tap="rotateAndScale">旋转并缩放</button>
        <button class="animation-button" bind:tap="rotateThenScale">旋转后缩放</button>
        <button class="animation-button" bind:tap="all">同时展示全部</button>
        <button class="animation-button" bind:tap="allInQueue">顺序展示全部</button>
        <button class="animation-button animation-button-reset" bind:tap="reset">还原</button>
      </view>
    </view>
  </view>
</view>
Page({
  data: {
    animation: [],
  },
  onReady: function () {
    this.animation = ty.createAnimation();
  },
  rotate: function () {
    this.animation.rotate(Math.random() * 720 - 360).step();
    let anim = this.animation.export();
    this.setData({ animation: anim });
    console.log(anim);
  },
  scale: function () {
    this.animation.scale(Math.random() * 2).step();
    let anim = this.animation.export();
    this.setData({ animation: anim });
    console.log(anim);
  },
  translate: function () {
    this.animation
      .translate(Math.random() * 100 - 50, Math.random() * 100 - 50)
      .step();
    let anim = this.animation.export();
    this.setData({ animation: anim });
    console.log(anim);
  },
  skew: function () {
    this.animation.skew(Math.random() * 90, Math.random() * 90).step();
    let anim = this.animation.export();
    this.setData({ animation: anim });
    console.log(anim);
  },
  rotateAndScale: function () {
    this.animation
      .rotate(Math.random() * 720 - 360)
      .scale(Math.random() * 2)
      .step();
    let anim = this.animation.export();
    this.setData({ animation: anim });
    console.log(anim);
  },
  rotateThenScale: function () {
    this.animation
      .rotate(Math.random() * 720 - 360)
      .step()
      .scale(Math.random() * 2)
      .step();
    let anim = this.animation.export();
    this.setData({ animation: anim });
    console.log(anim);
  },
  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();
    let anim = this.animation.export();
    this.setData({ animation: anim });
    console.log(anim);
  },
  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();
    let anim = this.animation.export();
    this.setData({ animation: anim });
    console.log(anim);
  },
  reset: function () {
    this.animation
      .rotate(0, 0)
      .scale(1)
      .translate(0, 0)
      .skew(0, 0)
      .step({ duration: 0 });
    let anim = this.animation.export();
    this.setData({ animation: anim });
    console.log(anim);
  },
});

 👉 立即免费领取开发资源,体验涂鸦 MiniApp 小程序开发。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IoT砖家涂拉拉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值