小程序模板網(wǎng)

WxTouchEvent 微信小程序手勢(shì)事件庫(kù)

發(fā)布時(shí)間:2018-04-18 09:50 所屬欄目:小程序開發(fā)教程
作者:斑駁光影,來自授權(quán)地址

由于微信小程序只能夠支持 tap,longtap,touchstart,touchmove,touchcancel,touchend時(shí)間,對(duì)于比較復(fù)雜的事件只能自己實(shí)現(xiàn)
因此自己對(duì) alloyFinger庫(kù)進(jìn)行了改造,開發(fā)了時(shí)候微信小程序手勢(shì)事件庫(kù)WxTouchEvent,使用 ES
6進(jìn)行編寫,手勢(shì)庫(kù)支持以下事件

  • touchStart

  • touchMove

  • touchEnd

  • touchCancel

  • multipointStart

  • multipointEnd

  • tap

  • doubleTap

  • longTap

  • singleTap

  • rotate

  • pinch

  • pressMove

  • swipe

使用

由于和微信小程序強(qiáng)綁定,因此需要在元素上面綁定好所有的事件,書寫比較麻煩,因此建議對(duì)于原生支持的使用原生去解決,只有當(dāng)需要 pinch,rotate,swipe 等特殊事件才使用這個(gè)事件庫(kù)實(shí)現(xiàn)

安裝 npm i wx-touch-event --save , 或者直接從 git 庫(kù) checkout 出來

綁定方法

*.wxml

在wxml中對(duì)需要監(jiān)聽時(shí)間的元素綁定 touchstart、touchmove、touchend、touchcancel四個(gè)事件
頁面書寫成

    <view class="info-list" 
          bindtouchstart="touchStart"
          bindtouchmove="touchMove"
          bindtouchend="touchEnd"
          bindtouchcancel="touchCancel"
        >
        
    </view>

*.js

在js邏輯層需要實(shí)例化WxTouchEvent, 實(shí)例中有start、move、end、cancel對(duì)應(yīng)\*.wxml綁定的bindtouchstart,bindtouchmove,bindtouchend,bindtouchcancel,需要將事件的回調(diào)函數(shù)一一對(duì)應(yīng),
書寫成:

 
import WxTouchEvent from "wx-touch-event";

let infoListTouchEvent = new WxTouchEvent();//在 Page外實(shí)例化函數(shù),可以直接復(fù)制給 Page 中的回調(diào)函數(shù)
Page({
    onLoad: function() {
        this.infoListTouchEvent = infoListTouchEvent;
        this.infoListTouchEvent.bind({//初始化后綁定事件
            swipe: function(e) {
                console.log(e);
            },
            doubleTap: function(e) {
                console.log(e);
            },
            tap: function(e) {
                console.log(e);
            }.bind(this),
            longTap: function(e) {
                console.log(e);
            },
            rotate: function(e) {
                console.log(e)
            }.bind(this),
            pinch: function(e) {
                console.log(e);
            }

        })
    },
    touchStart: infoListTouchEvent.start.bind(infoListTouchEvent),
    touchMove: infoListTouchEvent.move.bind(infoListTouchEvent),
    touchEnd: infoListTouchEvent.end.bind(infoListTouchEvent),
    touchCancel: infoListTouchEvent.cancel.bind(infoListTouchEvent),

});
 


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