小程序模板網(wǎng)

微信小程序animation循環(huán)動畫效果--讓云朵飄

發(fā)布時間:2017-12-25 11:35 所屬欄目:小程序開發(fā)教程

微信小程序提供了實(shí)現(xiàn)動畫的api——animation,但卻不能循環(huán)播放,都是一次性的,動完就Over了,下面提供一個用微信小程序的animation來實(shí)現(xiàn)循環(huán)動畫的玩具,拋磚引玉,希望大家能想出更好的方法來實(shí)現(xiàn)真正的循環(huán)。 ...

 
 
 

微信小程序提供了實(shí)現(xiàn)動畫的api——animation,但卻不能循環(huán)播放,都是一次性的,動完就Over了,下面提供一個用微信小程序的animation來實(shí)現(xiàn)循環(huán)動畫的玩具,拋磚引玉,希望大家能想出更好的方法來實(shí)現(xiàn)真正的循環(huán)。說是玩具是因?yàn)檫@個循環(huán)動畫通過js腳本的setInterval來實(shí)現(xiàn)的,但’setInterval’在實(shí)際運(yùn)行中會出現(xiàn)越來越嚴(yán)重的延遲,這是由于js的單線程運(yùn)行模式所決定的(具體可以搜本關(guān)資料看),所以動畫間隙并不是那么流暢,所以先玩玩吧,讓我們來實(shí)現(xiàn)讓云朵飄……

截圖如下:

讓云朵飄 
實(shí)現(xiàn)代碼:

index.wxml

<view class="clouds">
      <image animation="{{animationCloudData}}"  class="yun1" src="../../img/yun1.png"></image>
    </view>

index.js

  onReady: function () {
    // 頁面渲染完成
    // 實(shí)例化一個動畫
    var that = this;
    var i = 0
    var ii = 0

    var animationData = wx.createAnimation({
      duration: 1000, // 默認(rèn)為400     動畫持續(xù)時間,單位ms
      timingFunction: 'ease-in-out',
      //transformOrigin: '4px 91px'
    });

    var animationCloudData = wx.createAnimation({
      duration: 1000, // 默認(rèn)為400     動畫持續(xù)時間,單位ms
      timingFunction: 'ease-in-out',
      //transformOrigin: '4px 91px'
    });

    // 順序執(zhí)行,當(dāng)已經(jīng)執(zhí)行完上面的代碼就會開啟定時器
    // 循環(huán)執(zhí)行代碼
    //dotAnFun = setInterval(function () {});    
    /*setInterval(function () {
      // 動畫腳本定義
      //animationData.rotate(6 * (++i)).step()
      //animationData.scale(2, 2).rotate(45).step().scale(1, 1).step();
      animationData.translateY(10).step({ duration: 500 }).translateY(-10).step({ duration: 500 });

      // 更新數(shù)據(jù)
      that.setData({
        // 導(dǎo)出動畫示例
        animationData: animationData.export(),
        //animationCloudData: animationCloudData.export(),        
      })

      ++i;
      console.log(i);
    }.bind(that), 2000);//循環(huán)時間 這里1000是1秒
    */


    //動畫的腳本定義必須每次都重新生成,不能放在循環(huán)外
    animationCloudData.translateX(200).step({ duration: 5000 }).translateX(0).step({ duration: 5000 });

    // 更新數(shù)據(jù)
    that.setData({
      // 導(dǎo)出動畫示例
      //animationData: animationData.export(),
      animationCloudData: animationCloudData.export(),
    })

    setInterval(function () {
      //動畫的腳本定義必須每次都重新生成,不能放在循環(huán)外
      animationCloudData.translateX(300).step({ duration: 5000 }).translateX(-100).step({ duration: 5000 });

      // 更新數(shù)據(jù)
      that.setData({
        // 導(dǎo)出動畫示例
        //animationData: animationData.export(),
        animationCloudData: animationCloudData.export(),
      })
      ++ii;
      console.log(ii);

    }.bind(that),10000);//3000這里的設(shè)置如果小于動畫step的持續(xù)時間的話會導(dǎo)致執(zhí)行一半后出錯

  }

index.wxss

.clouds{
  margin-top:320rpx; 
}
.yun1{
  width:320rpx;
  height: 120rpx;

}
附:參考備用:

/*
    var that = this;
    // 頁面渲染完成
    //實(shí)例化一個動畫
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease',
    })

    this.animation = animation

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

    //導(dǎo)出動畫
    this.setData({
      animationData: animation.export()
    })

    var i = 0;
    // 順序執(zhí)行,當(dāng)已經(jīng)執(zhí)行完上面的代碼就會開啟定時器
    /*setTimeout(function () {
      that.setData({
        animationData: animation.export()
      });

      i++;
      console.log(i);
    }, 1000);*/

    /*setInterval(function () {
      //循環(huán)執(zhí)行代碼 
        that.setData({
          animationData: animation.export()
        });

      i++;
      console.log(i); 
    }, 1000) //循環(huán)時間 這里是1秒   

  }*/


易優(yōu)小程序(企業(yè)版)+靈活api+前后代碼開源 碼云倉庫:starfork
本文地址:http://www.u-renovate.com/wxmini/doc/course/18237.html 復(fù)制鏈接 如需定制請聯(lián)系易優(yōu)客服咨詢:800182392 點(diǎn)擊咨詢
QQ在線咨詢
AI智能客服 ×