Share a personal experience in developing small programs

Y2J
Release: 2017-05-04 09:34:04
Original
13805 people have browsed it

WeChat mini programs have been out for a while. 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.

First of all, let’s explain and develop our own project step by step. Below is a screenshot of a WeChat app

After seeing the picture above, friends have a general understanding. This is in the debugging tool, and 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 developing other apps when I have time in the future. Related examples.

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 image files, etc. The following is a screenshot of the overall file structure of the WeChat app

1.app.js is mainly globally public The file where the js method declaration and call are located

2.app.json is the entire configuration file of the mini program, so some pages require registration here, otherwise access is not allowed (as shown in the figure 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. Each page can add four types. Type files, .json, .wxss, .wxml, .js (as shown in the figure below)

5.utils is where our public js is stored, because the WeChat applet requires that in each js file The method cannot be directly referenced or called, and must be exported using the module.exports method, 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:

2)pages page structure:

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.

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

The title “Let’s Luck” at the top is It is defined in the index.json file. Each file can be defined with a different .json. Of course, the code can also dynamically change it.

It’s very simple, the title is just like this Simple appears.

1) Next, look at the horizontal scrolling banner,

##index.wxml describes it like this

So what is swiper? The WeChat applet help document explains this: swiper slider view container

Attribute nameTypeDefault valueDescriptionindicator-dotsBooleanfalseWhether to display panel indicator dotsautoplayBooleanfalseWhether to switch automaticallycurrentNumber0The index of the current pageintervalNumber5000Automatic switching time intervaldurationNumber500Sliding animation durationcircularBooleanfalseWhether to use connecting slidingbindchangeEventHandleThe change event will be triggered when current changes, event.detail = {current: current}##

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

swiper-item

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

 1 示例代码: 2  3 <swiper indicator-dots="{{indicatorDots}}" 4   autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}"> 5   <block wx:for="{{imgUrls}}"> 6     <swiper-item> 7       <image src="{{item}}" class="slide-image" width="355" height="150"/> 8     </swiper-item> 9   </block>10 </swiper>11 <button bindtap="changeIndicatorDots"> indicator-dots </button>12 <button bindtap="changeAutoplay"> autoplay </button>13 <slider bindchange="intervalChange" show-value min="500" max="2000"/> interval14 <slider bindchange="durationChange" show-value min="1000" max="10000"/> duration15 Page({16   data: {17     imgUrls: [18       &#39;http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg&#39;,19       &#39;http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg&#39;,20       &#39;http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg&#39;21     ],22     indicatorDots: false,23     autoplay: false,24     interval: 5000,25     duration: 100026   },27   changeIndicatorDots: function(e) {28     this.setData({29       indicatorDots: !this.data.indicatorDots30     })31   },32   changeAutoplay: function(e) {33     this.setData({34       autoplay: !this.data.autoplay35     })36   },37   intervalChange: function(e) {38     this.setData({39       interval: e.detail.value40     })41   },42   durationChange: function(e) {43     this.setData({44       duration: e.detail.value45     })46   }47 })
Copy after login

View Code

After reading the official document above, you can clearly know that this is our similar It is the same as the banner sliding plug-in used in writing HTML. You can just take it and use it. How convenient.

We also use parameter binding in our project, and the relevant output parameters

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

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

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

2) City selection and switching

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 documentation related to animation on the official website

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:

##duration IntegerNoAnimation duration, unit ms, default value 400##timingFunctiondelaytransformOrigin
var animation = wx.createAnimation({
  transformOrigin: "50% 50%",
  duration: 1000,
  timingFunction: "ease",
  delay: 0})
Copy after login
animation
ParameterTypeRequiredDescription
String NoDefine 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"
IntegerNoAnimation delay time, unit ms, Default value 0
StringNoSet transform-origin, the default is "50% 50% 0"

Animation instances can call the following methods to describe animations. After the call is completed, they will return to themselves and support chain call writing.

Style:

MethodopacitybackgroundColorwidthheighttopleft#bottomright

Rotation:

ParametersDescription
valueTransparency, parameter range 0~1
color Color value
lengthLength value, if Number is passed in, px is used by default, other custom units can be passed in The length value of
length length value, if Number is passed in, px is used by default, and the length of other custom units can be passed in Value
lengthLength value, if Number is passed in, px is used by default, and length values ​​in other custom units can be passed in
lengthThe length value, if Number is passed in, px is used by default, and length values ​​in other custom units can be passed in
lengthThe length value, if Number is passed in, px is used by default, and length values ​​in other custom units can be passed in
lengthThe length value, if Number is passed in, px is used by default, and length values ​​in other custom units can be passed in
MethodParametersDescription
rotatedegThe range of deg is -180~180, rotate one deg angle clockwise from the origin
rotateXdegThe range of deg is -180~180, rotate one deg angle on the X-axis
rotateYdeg The range of deg is -180~180, rotate a deg angle on the Y axis
rotateZdegThe range of deg is -180~180, on Rotate the Z axis by one deg angle
rotate3d(x,y,z,deg)Same as transform-function rotate3d

Zoom:

scaleYscaleZscale3dOffset:
MethodParametersDescription
scalesx,[sy]With one parameter, it means scaling the sx multiple on the X-axis and Y-axis at the same time; with two parameters, it means scaling the Scale sx multiples on the X-axis and sy multiples on the Y-axis
scaleXsxScale sx multiples on the
syScale the sy multiple on the Y axis
szScale sy multiples on the Z axis
(sx,sy,sz)Scale sx multiples on the X axis, on the Y axis Scale the sy multiple, scale the sz multiple on the Z axis

Method##translatetx,[ty]When there is one parameter, it means in The X-axis offset is tx, the unit is px; when there are two parameters, it means the X-axis offset is tx, and the Y-axis offset is ty, the unit is px. translateXtx Offset tx on the X axis, unit pxtranslateYty Offset tx on Y axis, unit pxtranslateZtz Offset on Z axis Move tx, unit pxtranslate3d(tx,ty,tz)Offset tx on the X axis, offset ty on the Y axis , offset tz on the Z axis, unit pxTilt:
Parameter Description

MethodParameters Descriptionskewax,[ay]Parameter range -180~180; When there is one parameter, the Y-axis coordinate remains unchanged, and the X-axis coordinate is tilted clockwise by ax degrees; when there are two parameters, the X-axis is tilted by ax degrees, and the Y-axis is tilted by ay degreesskewXaxParameter range -180~180; Y-axis coordinate remains unchanged, X-axis coordinate is tilted clockwise by a degreeskewY ayParameter range -180~180; Matrix transformation: Method
Parameters

DescriptionSame as transform-function matrix##matrix3dSame as transform-function matrix3d

动画队列

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

示例:

 1 <view animation="{{animationData}}" style="background:red;height:100rpx;width:100rpx"></view> 2 Page({ 3   data: { 4     animationData: {} 5   }, 6   onShow: function(){ 7     var animation = wx.createAnimation({ 8       duration: 1000, 9         timingFunction: &#39;ease&#39;,10     })11 12     this.animation = animation13 14     animation.scale(2,2).rotate(45).step()15 16     this.setData({17       animationData:animation.export()18     })19 20     setTimeout(function() {21       animation.translate(30).step()22       this.setData({23         animationData:animation.export()24       })25     }.bind(this), 1000)26   },27   rotateAndScale: function () {28     // 旋转同时放大29     this.animation.rotate(45).scale(2, 2).step()30     this.setData({31       animationData: this.animation.export()32     })33   },34   rotateThenScale: function () {35     // 先旋转后放大36     this.animation.rotate(45).step()37     this.animation.scale(2, 2).step()38     this.setData({39       animationData: this.animation.export()40     })41   },42   rotateAndScaleThenTranslate: function () {43     // 先旋转同时放大,然后平移44     this.animation.rotate(45).scale(2, 2).step()45     this.animation.translate(100, 100).step({ duration: 1000 })46     this.setData({47       animationData: this.animation.export()48     })49   }50 })
Copy after login

View Code

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

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

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度,以原点为中心

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

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

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

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

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

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

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

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

animation样式:

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

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

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

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

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

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

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

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

对应的wxml界面:

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

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

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

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

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

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

好了,我们来看看代码:

定义三个动画:

 1     animation1 = wx.createAnimation({ 2           duration: 300, 3           timingFunction: &#39;linear&#39;, 4           transformOrigin: "50%,50%" 5         }) 6  7         this.setData({ 8           animationData: animation1.export() 9         })10 11          animation2 = wx.createAnimation({12           duration: 300,13           timingFunction: &#39;linear&#39;14         })15 16         this.setData({17           animationSourceCity: animation2.export()18         })19 20          animation3 = wx.createAnimation({21           duration: 300,22           timingFunction: &#39;linear&#39;23         })24 25         this.setData({26           animationDestCity: animation3.export()27         })
Copy after login

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

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

下面我们先来说说animation2,animation3

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

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

 1 animation2.left(&#39;600rpx&#39;).step() 2         this.setData({ 3         animationSourceCity: animation2.export() 4       }) 5  6       setTimeout(function(){ 7         animation2.left(&#39;30rpx&#39;).step({duration: 0, transformOrigin: "50%,50%",timingFunction: &#39;linear&#39;}) 8         that.setData({ 9            animationSourceCity: animation2.export()10         })11       },285)12 13       animation3.right(&#39;580rpx&#39;).step()14         this.setData({15         animationDestCity: animation3.export()16       })17       18        setTimeout(function(){19         animation3.right(&#39;30rpx&#39;).step({duration: 0, transformOrigin: "50%,50%",timingFunction: &#39;linear&#39;})20         that.setData({21            animationDestCity: animation3.export()22         })23       },285)
Copy after login

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

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

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

这个时候600rpx只是粗略的计算,并不是真正的精确定位,原因上面我们解释很清楚了,移动600rpx所需时间是300ms,紧接着,如果这样的结束的话,很可能位置会错位,所以我们要写一个"特殊的动画"setTimeout(function(){
Copy after login
<em>        animation2.left(&#39;30rpx&#39;).step({duration: 0, transformOrigin: "50%,50%",timingFunction: &#39;linear&#39;})        that.setData({            animationSourceCity: animation2.export()         })
    },285)<br/><br/>这个动画表示,在285ms后,将要在0s时间完成"复位",在0s时间,估计没有人会查觉得到,呵呵,复位的好处,太多了,如果不复位,意味,我们的元素真的交换了,那样事件也给交换了,给我们带来了<br/>太多的麻烦,<br/>而复位,可以让我们仅仅交换了”城市文本“而不是所有。哈哈~开心,只所以定义285ms,是给一个很短的机会,让人看不到复位的执行,毕竟上面的300ms的水平动画还没有执行完嘛<br/>而真正的换交在下面的一句话</em>
Copy after login
  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点的位置)

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

图二是点击旋转图片后,自己处于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度,你眼神再好,也查觉不到,嘿嘿。。。

完整的旋转代码如下:

 1 animation1.rotate(180).step() 2       3      this.setData({ 4         animationData: animation1.export() 5       }) 6         7       var that=this; 
 8       setTimeout(function(){ 9         animation1.rotate(0).step({duration: 0, transformOrigin: "50%,50%",timingFunction: &#39;linear&#39;})10         that.setData({11            animationData: animation1.export()12         })13       },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)的搭建、交互等等,期待着我们一个一个的解决呢,这些我准备将在后面的文章陆陆续续的写出,敬请关注,谢谢。 

matrix(a,b,c,d,tx,ty)

The above is the detailed content of Share a personal experience in developing small programs. 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