動畫製作animation

  1. wxml文件中,圖片加入動畫屬性,點擊圖片的時候執行動畫
    在這裏插入圖片描述
    附上wxml完整代碼
<!--index.wxml-->
<view class="container">
  <view class="userinfo">
    <button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 獲取頭像暱稱 </button>
    <block wx:else>
      <image  animation='{{animationData}}' bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
      <text class="userinfo-nickname">{{userInfo.nickName}}</text>
    </block>
  </view>
  <view class="usermotto">
    <text class="user-motto">{{motto}}</text>
  </view>
</view>

  1. js文件中,定義變量animationData,觸發事件中,創建動畫和設置動畫動作
    在這裏插入圖片描述
    在這裏插入圖片描述
    附上js文件完整代碼
//index.js
//獲取應用實例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    animationData:[]  //保存動畫的變量
  },

  //事件處理函數
  bindViewTap: function() {
    //創建動畫
    var animation = wx.createAnimation({ //調用函數,返回animation
      duration: 3000, //持續時間
      timingFunction: 'linear', //播放方式
      delay:3000,  //延遲
    });

    //執行動畫的動作
    animation.rotate(90).step();  //順時針旋轉90度

    //賦值新數據給animationData,開始動畫
    this.setData({  
      animationData: animation.export()
    });
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由於 getUserInfo 是網絡請求,可能會在 Page.onLoad 之後才返回
      // 所以此處加入 callback 以防止這種情況
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在沒有 open-type=getUserInfo 版本的兼容處理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})

  1. 效果實現
    在這裏插入圖片描述
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章