Home > WeChat Applet > Mini Program Development > Detailed instructions for WeChat applet production and animation (animation style)

Detailed instructions for WeChat applet production and animation (animation style)

高洛峰
Release: 2017-03-15 15:57:15
Original
5438 people have browsed it

This article mainly introduces relevant information on WeChat mini program production and animation (animation style). Here is a detailed explanation of mini program production and introduction to the animation part. Friends in need can refer to it. Next

WeChat Mini Program Production

Realization renderings:

Detailed instructions for WeChat applet production and animation (animation style)

WeChat Mini Program has also been released for a while It's time. I have recently written several WeChat mini program projects. Today I will talk about my feelings.

First develop a WeChat mini program, the most important thing is to operate it for the company, because when applying for appid (WeChat mini program ID number), you need to fill in relevant company certification information such as business license Wait

The next step is to use a QQ account or WeChat ID that has not yet opened a public account to register a WeChat mini program account.

Finally, download the WeChat applet development tool.

Because here, we focus more on how to develop some apps rather than the Kepu WeChat applet, so we will not explain too much here. For detailed instructions, you can go to the official website help documentation.

After seeing the picture above, friends have a general understanding. This is in the Debugging tool. Some effects are not as good as on the real machine.

Due to the development, I thought the picture was not very smooth. In fact, it was completely beyond my expectation. The animation effect is very smooth, comparable to ios and andriod apps. I will talk about development when I have time in the future Related examples of other apps.

Before introducing this article, it is assumed that the user has read the relevant documents of the WeChat applet.

This project is basically based on the original file structure of WeChat, and there is no extra file structure to add, because the WeChat applet stipulates that the project file size cannot exceed 1M, requiring us to try our best to Compressed applet code or other picture files, etc. The following is a screenshot of the overall WeChat app file structure

Detailed instructions for WeChat applet production and animation (animation style)

1.app.js Mainly the global public js method declaration and the file where it is called

2.app.json is the entire configuration file of the mini program, so some pages are You need to register here, otherwise access is not allowed (as shown in the picture below)

3.app.wxss is the global css file of the mini program. It is best to write public css here.

4.pages corresponds to all pages. For each page, four types of files can be added, .json, .wxss, .wxml, .js (as shown in the figure below) (Indicated)

5.utils is where our public js is stored. Because the WeChat applet requires that the methods in each js file cannot be directly referenced or called, they must be module.exports method is exported, so that the .js file under pages can call the js method we wrote here. Special attention should be paid to this point

1)app.json page configuration and registration:

Detailed instructions for WeChat applet production and animation (animation style)

## 2)pages page structure:

Detailed instructions for WeChat applet production and animation (animation style)

Let’s start to explain each page in detail

1. Homepage

The homepage is divided into four files, as shown in the figure below, The specific page functions have been mentioned above.

Detailed instructions for WeChat applet production and animation (animation style)

Let’s take a look at the effect of index.wxml

Detailed instructions for WeChat applet production and animation (animation style)

The top "Let's Luck" title is defined under the index.json file. Each file can be defined with a different .json. Of course, the code can also be changed dynamically. It

Detailed instructions for WeChat applet production and animation (animation style)

is very simple, the title just appears.

1) Next, look at the horizontal scrolling banner,

Detailed instructions for WeChat applet production and animation (animation style)

index.wxml is described like this

Detailed instructions for WeChat applet production and animation (animation style)

So what is swiper? The WeChat applet help document explains this: swiper sliderViewContainer

TypeDefault valueInstructionsindicator-dotsBooleanfalseWhether to display the panel indicator point##autoplaycurrentintervaldurationcircularbindchangedlEvent

Note: Only <swiper-item></swiper-item> components can be placed in it, other nodes will be automatically deleted.

swiper-item

can only be placed in the <swiper></swiper> component, and the width and height are automatically set to 100%.

Sample code:

<swiper indicator-dots="{{indicatorDots}}"
 autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}">
 <block wx:for="{{imgUrls}}">
  <swiper-item>
   <image src="{{item}}" class="slide-image" width="355" height="150"/>
  </swiper-item>
 </block>
</swiper>
<button bindtap="changeIndicatorDots"> indicator-dots </button>
<button bindtap="changeAutoplay"> autoplay </button>
<slider bindchange="intervalChange" show-value min="500" max="2000"/> interval
<slider bindchange="durationChange" show-value min="1000" max="10000"/> duration
Page({
 data: {
  imgUrls: [
   &#39;http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg&#39;,
   &#39;http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg&#39;,
   &#39;http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg&#39;
  ],
  indicatorDots: false,
  autoplay: false,
  interval: 5000,
  duration: 1000
 },
 changeIndicatorDots: function(e) {
  this.setData({
   indicatorDots: !this.data.indicatorDots
  })
 },
 changeAutoplay: function(e) {
  this.setData({
   autoplay: !this.data.autoplay
  })
 },
 intervalChange: function(e) {
  this.setData({
   interval: e.detail.value
  })
 },
 durationChange: function(e) {
  this.setData({
   duration: e.detail.value
  })
 }
})
Copy after login

After reading the official document above, you can clearly know that this is our Similar to the banner sliding plug-in used in writing HTML, you can use it as soon as you bring it over, how convenient it is.

In our project, we also use parameter binding. The output related parameters

parameters are defined in the index.js pages({...}) method

Detailed instructions for WeChat applet production and animation (animation style)

Why do we need to bind parameters? Why not just write the parameters directly? There are too many benefits. We can't write pictures to death and request pictures from the server. At the same time, we can easily control our relevant parameters to change the behavior of swiper, etc.

As for parameter binding, the official website makes it very clear, so I won’t explain it here.

2) City selection and switching

Detailed instructions for WeChat applet production and animation (animation style)

This seems very simple, but in fact it is very troublesome. If you are not familiar with animation, it may be distressing. Ichiban.

The above animation is very smooth, maybe because the screen capture tool is not very good, but you don’t need to worry about this.

When we click on the "exchange circle" in the middle, the "departure city" and "arrival city" are exchanged with each other. They do not change immediately, but there is a "displacement" effect in the middle. At the same time, the "exchange" Circle" also needs to be rotated 180 degrees.

This experience immediately makes you feel "higher". Haha, isn't it? Below we implement it in detail.

Let’s first review the document description related to the official website animation

wx.createAnimation(OBJECT)

Create an animation instance animation. Call the instance's methods to describe the animation. Finally, the animation data is exported through the export method of the animation instance and passed to the animation property of the component.

Note: export Each time the method is called, the previous animation operation will be cleared

OBJECT parameter description:

##AttributeName
Boolean false Yes Automatically switch
Number 0 The index of the current page
Number 5000 Automatic switching interval
Number 500 Sliding animation duration
Boolean false Whether to use connecting sliding
EventHane Triggered when current changes change , event.detail = {current: current}
##durationNoAnimation duration, unit ms, default value 400timingFunctionNoDefine the effect of the animation, the default value is "linear", valid values: "linear", "ease", "ease-in", "ease- in-out","ease-out","step-start","step-end"delayIntegerNoAnimation delay time, unit ms, default value 0transformOriginStringNoSet transform -origin, default is "50% 50% 0"


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

animation

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

样式:

ParameterTypeRequiredDescription
Integer
String
方法参数说明
opacityvalue透明度,参数范围 0~1
backgroundColorcolor颜色值
widthlength长度值,如果传入 Number 则默认使用 px,可传入其他自定义单位的长度值
heightlength长度值,如果传入 Number 则默认使用 px,可传入其他自定义单位的长度值
toplength长度值,如果传入 Number 则默认使用 px,可传入其他自定义单位的长度值
leftlength长度值,如果传入 Number 则默认使用 px,可传入其他自定义单位的长度值
bottomlength长度值,如果传入 Number 则默认使用 px,可传入其他自定义单位的长度值
rightlength长度值,如果传入 Number 则默认使用 px,可传入其他自定义单位的长度值

旋转:

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

缩放:

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

偏移:

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

倾斜:

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

矩阵变形:

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

动画队列

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

示例:

<view animation="{{animationData}}" style="background:red;height:100rpx;width:100rpx"></view>
Page({
 data: {
  animationData: {}
 },
 onShow: function(){
  var animation = wx.createAnimation({
   duration: 1000,
    timingFunction: &#39;ease&#39;,
  })

  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()
  })
 }
})
Copy after login

这里我并不想一个一个的介绍官方的动画说明文档,因为写的很清楚了,而是我想说下一些关于动画的机制

不管是位移,缩放,旋转,可能都会涉及到三个轴,那就是x,y,z,轴,这三个轴大致这样的如下图

Detailed instructions for WeChat applet production and animation (animation style)

x轴是水平的,y轴在垂直方向上,而z轴,是"指向我们的方向"的一个轴,这点必须清楚,不然动画的很多东西,你就没办法理解了。

好了,我们再来回过头来看看官网的几个动画方法。

旋转:1.rotate(deg),2.rotateX(deg),3.rotateY(deg),4.rotateZ(deg),5.rotate3d(x,y,z,deg)

1.rotate表示以原点在顺时针旋转一个度数deg范围在-180~180

假如我们要让一个图片,顺时针旋转90度,以原点为中心

可能刚开始图片这样排列的如下图

Detailed instructions for WeChat applet production and animation (animation style)

旋转后,由图A顺时针旋转90度至图B,它是在一个X与Y的平面上与Z轴成垂直90度来顺时针旋转的。

Detailed instructions for WeChat applet production and animation (animation style)

由上述可以看出,图片的左上角坐标是(x:0,y:0,z:0);而我们要旋转一个图片,一般不希望在左上角做为旋转点,最多的情况下,就是以图片的中心点为旋转点(x:50%,y:50%,z:0) z坐标是指向我们的坐标,就像css里的z-index一样,我们应该把它设为0,即使你设为任何一个数字,你的视角差也感不到任何不同,因为,图片的z轴是垂直我们视线的,故一般设置为0。

就像下面如图所示,可能是我们希望的旋转效果:

Detailed instructions for WeChat applet production and animation (animation style)

不好意思呀,用QQ绘图工具绘制,可能效果不太好,但是大致的表达了这种示意图,

上图描述了,由图片A由中心点,旋转90度后的效果,那么如何初始化,让图片的原点由(x:0,y:0,z:0)更换为(x:50%,y:50%,z:0)呢?回过头来看下官网教程的wx.createAnimation(OBJECT)方法

Detailed instructions for WeChat applet production and animation (animation style)

其中属性transformOrigin 已说明,默认为图片的中心点,可能是作者的初衷也这么认为的,旋转应该以”元素“的中心点来操作应用比较多点,这是合情可理的

至此,我们旋转一个图片得了到大致的思路。其它的以X轴,Y轴,Z轴旋转与些类似,不在累述。

animation样式:

Detailed instructions for WeChat applet production and animation (animation style)

如何让一个元素从一个位置从A点移到B点呢?可能通过上述的样式属性在改变”元素“的top bottom left right 达到效果,

当然也可以通过其它动画方法来改变,如偏移 translate(x,y,z)。

通过top bottom left right 样式属性来实现动画,前提是,这个”元素“一定是相对定位或者绝对定位的,不然是不出效果的,这和写css里的position:absolute相同的原理。

如果要让一个元素或图片从A点平移至B点,就像下图所以示

Detailed instructions for WeChat applet production and animation (animation style)

假如初始A坐标为(x:10px,y:0px,z:0px)移至B点坐标(x:120px,y:0px,z:0px),那样我们只需改变元素的left或者right即可,

同理,可以用bottom,top来改变y坐标。

好了,到此为止,我们项目的所需动画可能要用到的效果都基本上有了思路。那么下面我们就来实现它。

首先,我们在”出发城市'与"到达城市"以及"旋转图片"定义如下:

Detailed instructions for WeChat applet production and animation (animation style)

对应的wxml界面:

Detailed instructions for WeChat applet production and animation (animation style)

然后,我们为注意到在index.wxss(如下图)里给了绝对定位,目的就是想用left或right来动画交换城市

Detailed instructions for WeChat applet production and animation (animation style)

这里注意一点,animationsSourceCity初始化的时候,css里用了left, 动画时,必须用它的left来"位移",而不是right

不然会看不到效果,这点,在玩css3动画的时候,就遇到过。同理,下面的animationsDestCity只能用right来"位移"。

为什么有的朋友会想在初始化的时候用left可动画的时候想right的呢?可能考虑到元素的准确的定位原因,毕竟,精确的定位不是一件很容易的事情。 为什么这么说呢?因为考虑到app在其它屏上显示。

Detailed instructions for WeChat applet production and animation (animation style)

从上面的截图可以看到,现实中的问题,中间这块,宽与高是用了px,就是说,我们不希望中间这个旋转按扭自适应不同的手机屏,而希望他能够保持不变。这个时候,如果我们仅仅用left来平移"出发城市"至"到达城市"的坐标处,可能不管你用px还是rpx或其它单位,都达不到精确定位了(为什么?)。

这个时候,换个角度来思考下,我们不需要让它精确的位移至“到达城市”,为什么这么说呢?在”出发城市“移至”到达城市“前的一点很短的时间内,我们让它在0s交换城市(也就是复位但文本内容已交换),因为0s互换城市文本内容,估计没有任何人可以发觉到的。这就需要一个“恰当的时间”。

好了,我们来看看代码:

定义三个动画:

animation1 = wx.createAnimation({
     duration: 300,
     timingFunction: &#39;linear&#39;,
     transformOrigin: "50%,50%"
    })

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

     animation2 = wx.createAnimation({
     duration: 300,
     timingFunction: &#39;linear&#39;
    })

    this.setData({
     animationSourceCity: animation2.export()
    })

     animation3 = wx.createAnimation({
     duration: 300,
     timingFunction: &#39;linear&#39;
    })

    this.setData({
     animationDestCity: animation3.export()
    })
Copy after login

animation1是旋转图片的动画定义(初始化,具体的参数官网说的很清楚,不多说)。

animation2与animation3分别是”出发城市“与”到达城市“定义

下面我们先来说说animation2,animation3

animation2要完成的是从left ”出发城市“水平移动至”到达城市“坐标

我们看看点击旋转图片时事件:

animation2.left(&#39;600rpx&#39;).step()
    this.setData({
    animationSourceCity: animation2.export()
   })

   setTimeout(function(){
    animation2.left(&#39;30rpx&#39;).step({duration: 0, transformOrigin: "50%,50%",timingFunction: &#39;linear&#39;})
    that.setData({
      animationSourceCity: animation2.export()
    })
   },285)

   animation3.right(&#39;580rpx&#39;).step()
    this.setData({
    animationDestCity: animation3.export()
   })
   
    setTimeout(function(){
    animation3.right(&#39;30rpx&#39;).step({duration: 0, transformOrigin: "50%,50%",timingFunction: &#39;linear&#39;})
    that.setData({
      animationDestCity: animation3.export()
    })
   },285)
Copy after login

我们来分析下上面的代码:

在初始化的时候,设置了动画完成时间duration:300ms,紧接着,点击图片开始水平移动600rpx

animation2.left(&#39;600rpx&#39;).step()
    this.setData({
    animationSourceCity: animation2.export()
   })
Copy after login

这个时候600rpx只是粗略的计算,并不是真正的精确定位,原因上面我们解释很清楚了,移动600rpx所需时间是300ms,紧接着,如果这样的结束的话,很可能位置会错位,所以我们要写一个"特殊的动画“,

"setTimeout(function(){
    animation2.left(&#39;30rpx&#39;).step({duration: 0, transformOrigin: "50%,50%",timingFunction: &#39;linear&#39;})
    that.setData({
      animationSourceCity: animation2.export()
     })
  },285)
Copy after login

这个动画表示,在285ms后,将要在0s时间完成"复位",在0s时间,估计没有人会查觉得到,呵呵,复位的好处,太多了,如果不复位,意味,我们的元素真的交换了,那样事件也给交换了,给我们带来了太多的麻烦,而复位,可以让我们仅仅交换了”城市文本“而不是所有。哈哈~开心,只所以定义285ms,是给一个很短的机会,让人看不到复位的执行,毕竟上面的300ms的水平动画还没有执行完嘛

而真正的换交在下面的一句话

var tempSourceCity=this.data.sourceCity
   var tempDestCity=this.data.destCity
   this.setData({
    sourceCity:tempDestCity,
    destCity:tempSourceCity
   })
Copy after login

同理,right也一样来现实,这里不多说了,有兴趣的可以尝试下。

下面我们来说说,交换按扭图片的旋转动画

如果在点击事件rotate里我们这样写入

animation1.rotate(180).step()
   
   this.setData({
    animationData: animation1.export()
   })
Copy after login

恩,看起来不错,我们尝试的时候,第一旋转了,然后第二次,第三次。。。并没有旋转。啊呀,愁人的事情又来了。我会不尽的报怨,小程序呀,你的bug又来了。

其实你看官网给出的例子也是如此,旋转一下,再也不旋转了,除非你刷新下页面。

报怨归报怨,纳闷归纳闷,问题还要是解决的。

这是不是我们自己的问题呢?一万个为什么。。。

不是!还记得,在css3动画的时候,确实也这样,我来画图解释下为什么!

图一、旋转前:(注意A点的位置)

Detailed instructions for WeChat applet production and animation (animation style)

图二、旋转180度后(注意A的位置)

Detailed instructions for WeChat applet production and animation (animation style)

图二是点击旋转图片后,自己处于180度状态,此时,再次点击此旋转图片,意味着,让它再次从0度旋转到180度,可是我们的代码是

animation1.rotate(180).step()
Copy after login

这行代码表示,让它在300ms(初始化创建的时间)内旋转到180度,而是此时已处理180度啦,你点击当然它不会再旋转了。它会不停报怨”我已在180度了呀,你还想怎么样?!...“

所以,此时,我们能不能直接再让旋转360度,那么它不就相对于180度后的状态又转了180度了吗?可是看看官网,旋转的范围是-180~180度,既使没有这么范围限制,那么我们也会折腾死,不是吗?每次都要180*2,180*3...,表示不服!

我想只要问题找到了,其实都很简单了,此时估计都有朋友想到了,就是直接让它归0度嘛,这个归0度的动画时间必须要短,不然就要让人看到了一个”倒旋转的过程“,哇,那多么的难看呀,OK,动画嘛,上面我们都有先例,0s复位到0度,你眼神再好,也查觉不到,嘿嘿。。。

完整的旋转代码如下:

animation1.rotate(180).step()
   
   this.setData({
    animationData: animation1.export()
   })
    
   var that=this; 
   setTimeout(function(){
    animation1.rotate(0).step({duration: 0, transformOrigin: "50%,50%",timingFunction: &#39;linear&#39;})
    that.setData({
      animationData: animation1.export()
    })
   },300)
Copy after login

意思是,在点击时候,在300ms内旋转180度,同时在300ms后执行一个在0s时间完成新的动画让它复位至0度,下次点击时,它就再次可以旋转了!

animation1.rotate(0).step({duration: 0, transformOrigin: "50%,50%",timingFunction: &#39;linear&#39;})//归0度”复位“
Copy after login

上面的思想并不难,就是有时候不好发现,或者说,没接触过动画的朋友,一时半时找不出问题所在,写在此,尽可能的让大家少走弯路。

好了,这部分的动画就全部完成了,下面我们还有首页的上下不间断滚动、类似苹果手机ios app的滑动、删除效果,以及https api(基于asp.net mvc)的搭建、交互等等,期待着我们一个一个的解决呢,这些我准备将在后面的文章陆陆续续的写出,敬请关注,谢谢。


The above is the detailed content of Detailed instructions for WeChat applet production and animation (animation style). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template