亚洲狼人综合,本道综合精品,在线看福利影,国产亚洲精品久久久玫瑰,日韩欧美精品在线观看,日韩在线国产,欧美乱码一区二区三区

微信小程序 觸控事件

2018-03-14 14:21:01 腳本之家  點(diǎn)擊量: 評(píng)論 (0)
》》》什么是事件事件是視圖層到邏輯層的通訊方式。事件可以將用戶的行為反饋到邏輯層進(jìn)行處理。事件可以綁定在組件上,當(dāng)達(dá)到觸發(fā)事件,就

》》》什么是事件

  1. 事件是視圖層到邏輯層的通訊方式。
  2. 事件可以將用戶的行為反饋到邏輯層進(jìn)行處理。
  3. 事件可以綁定在組件上,當(dāng)達(dá)到觸發(fā)事件,就會(huì)執(zhí)行邏輯層中對(duì)應(yīng)的事件處理函數(shù)。
  4. 事件對(duì)象可以攜帶額外信息,如id, dataset, touches。

事件的使用方式

在組件中綁定一個(gè)事件處理函數(shù)。

如bindtap,當(dāng)用戶點(diǎn)擊該組件的時(shí)候會(huì)在該頁(yè)面對(duì)應(yīng)的Page中找到相應(yīng)的事件處理函數(shù)。

<view id="tapTest" data-hi="MINA" bindtap="tapName"> Click me! </view> 

在相應(yīng)的Page定義中寫(xiě)上相應(yīng)的事件處理函數(shù),參數(shù)是event。

[html] view plain copy
 
  1. Page({  
  2.  tapName: function(event) {  
  3.  console.log(event)  
  4.  }  
  5. })  

 

可以看到log出來(lái)的信息大致如下:

[html] view plain copy
 
  1. {  
  2. "type":"tap",  
  3. "timeStamp": 1252,  
  4. "target": {  
  5.  "id":"tapTest",  
  6.  "offsetLeft": 0,  
  7.  "offsetTop": 0,  
  8.  "dataset": {  
  9.  "hi":"MINA"  
  10.  }  
  11. },  
  12. "currentTarget": {  
  13.  "id":"tapTest",  
  14.  "offsetLeft": 0,  
  15.  "offsetTop": 0,  
  16.  "dataset": {  
  17.  "hi":"MINA"  
  18.  }  
  19. },  
  20. "touches": [{  
  21.  "pageX": 30,  
  22.  "pageY": 12,  
  23.  "clientX": 30,  
  24.  "clientY": 12,  
  25.  "screenX": 112,  
  26.  "screenY": 151  
  27. }],  
  28. "detail": {  
  29.  "x": 30,  
  30.  "y": 12  
  31. }  
  32. }  

 

事件詳解

事件分類(lèi)

事件分為冒泡事件和非冒泡事件:

冒泡事件:當(dāng)一個(gè)組件上的事件被觸發(fā)后,該事件會(huì)向父節(jié)點(diǎn)傳遞。

非冒泡事件:當(dāng)一個(gè)組件上的事件被觸發(fā)后,該事件不會(huì)向父節(jié)點(diǎn)傳遞。

》》》事件分類(lèi)

  1. touchstart 手指觸摸
  2. touchmove 手指觸摸后移動(dòng)
  3. touchcancel 手指觸摸動(dòng)作被打斷,如彈窗和來(lái)電提醒
  4. touchend 手指觸摸動(dòng)作結(jié)束
  5. tap 手指觸摸后離開(kāi)
  6. longtap 手指觸摸后后,超過(guò)350ms離開(kāi)

》》》事件綁定

事件綁定的寫(xiě)法同組件的屬性,以 key、value 的形式。

  1. key 以bind或catch開(kāi)頭,然后跟上事件的類(lèi)型,如bindtap, catchtouchstart
  2. value 是一個(gè)字符串,需要在對(duì)應(yīng)的 Page 中定義同名的函數(shù)。不然當(dāng)觸發(fā)事件的時(shí)候會(huì)報(bào)錯(cuò)。 bind事件綁定不會(huì)阻止冒泡事件向上冒泡,catch事件綁定可以阻止冒泡事件向上冒泡。

上面簡(jiǎn)單介紹了小程序事件基礎(chǔ),是時(shí)候彰顯"事件"的威力:

  1. 單擊(tap)
  2. 雙擊(dbtap)
  3. 長(zhǎng)按(longtap)
  4. 滑動(dòng)
  5. 多點(diǎn)觸控

1.單擊

單擊事件由touchstart、touchend組成,touchend后觸發(fā)tap事件。

 

 
[html] view plain copy
 
  1. <view>  
  2.  <button type="primary"bindtouchstart="mytouchstart"bindtouchend="mytouchend"bindtap="mytap">點(diǎn)我吧</button>  
  3. </view>  
  4. mytouchstart: function(e){ console.log(e.timeStamp +'- touch start')  
  5. },mytouchend: function(e){ console.log(e.timeStamp +'- touch end')  
  6. },mytap: function(e){ console.log(e.timeStamp +'- tap')  
  7. }  

 

2.雙擊

雙擊事件由兩個(gè)單擊事件組成,兩次間隔時(shí)間小于300ms認(rèn)為是雙擊;微信官方文檔沒(méi)有雙擊事件,需要開(kāi)發(fā)者自己定義處理。

 

[html] view plain copy
 
  1. <view>  
  2.  <buttontypebuttontype="primary"bindtap="mytap">點(diǎn)我吧</button>  
  3. </view>  

 


 

 3.長(zhǎng)按

長(zhǎng)按事件手指觸摸后,超過(guò)350ms再離開(kāi)。

 

[html] view plain copy
 
  1. <view>  
  2.  <button type="primary"bindtouchstart="mytouchstart"bindlongtap="mylongtap"  
  3.  bindtouchend="mytouchend"bindtap="mytap">點(diǎn)我吧</button>  
  4. </view>  
  5. mytouchstart: function(e){ console.log(e.timeStamp +'- touch start')  
  6. },//長(zhǎng)按事件mylongtap: function(e){ console.log(e.timeStamp + '- long tap')  
  7.  },mytouchend:function(e){ console.log(e.timeStamp +'- touch end')  
  8. },mytap: function(e){ console.log(e.timeStamp +'- tap')  
  9. }  

 

單擊、雙擊、長(zhǎng)按屬于點(diǎn)觸事件,會(huì)觸發(fā)touchstart、touchend、tap事件,touchcancel事件只能在真機(jī)模擬,不多說(shuō)了。

事件 觸發(fā)順序
單擊 touchstart → touchend → tap
雙擊 touchstart → touchend → tap → touchstart → touchend → tap
長(zhǎng)按 touchstart → longtap → touchend → tap

 

4.滑動(dòng)

手指觸摸屏幕并移動(dòng),為了簡(jiǎn)化起見(jiàn),下面以水平滑動(dòng)和垂直滑動(dòng)為例。 滑動(dòng)事件由touchstart、touchmove、touchend組成

 

坐標(biāo)圖:

 

  1. 以屏幕左上角為原點(diǎn)建立直角坐標(biāo)系。第四象限為手機(jī)屏幕,Y軸越往下坐標(biāo)值越大(注意跟數(shù)學(xué)象限的區(qū)別)。
  2. 假設(shè)A點(diǎn)為touchstart事件觸摸點(diǎn),坐標(biāo)為A(ax,ay),然后手指向上滑動(dòng)到點(diǎn)B(bx,by),就滿足條件by < ay;
  3. 同理,向右滑動(dòng)到C(cx,cy),滿足cx > ax;向下滑動(dòng)到D(dx,dy),滿足dy > ay;向左移動(dòng)到E(ex,ey)滿足ex < ax.
  4. 計(jì)算線段AB在Y軸上投影長(zhǎng)度為m,在X軸上的投影長(zhǎng)度為n
  5. 計(jì)算r = m/n,如果r > 1,視為向上滑動(dòng)。
  6. 同理計(jì)算線段AC,AD,AE在Y軸投影長(zhǎng)度與X軸的投影長(zhǎng)度之比,得出向右向下向左的滑動(dòng)。

以上沒(méi)考慮r為1的情況。

[html] view plain copy
 
  1. <view>  
  2.  <buttontypebuttontype="primary"bindtouchstart="mytouchstart"bindtouchmove="mytouchmove">點(diǎn)我吧</button>  
  3. </view>  

 

 

[html] view plain copy
 
  1. mytouchstart: function (e) {  
  2.     var that = this;  
  3.     //開(kāi)始觸摸,獲取觸摸坐標(biāo)  
  4.     console.log(e)  
  5.     that.setData({ startpoint: [e.touches[0].pageX, e.touches[0].pageY] });  
  6. },  
  7. //觸摸點(diǎn)移動(dòng)  
  8. mytouchmove: function (e) {  
  9.     //當(dāng)前觸摸點(diǎn)坐標(biāo)  
  10.     var that = this;  
  11.     var curPoint = [e.touches[0].pageX, e.touches[0].pageY];  
  12.     var startpoint = that.data.startpoint;  
  13.     console.log(startpoint)  
  14.     console.log(curPoint)  
  15.     //比較pagex值  
  16.     if (curPoint[0] < startpoint[0]) {  
  17.         if (Math.abs(curPoint[0] - startpoint[0]) >= Math.abs(curPoint[1] - startpoint[1])) {  
  18.             console.log(e.timestamp + '-touch left move')  
  19.             that.setData({  
  20.                 dellStyle: "dellList"  
  21.             })  
  22.         } else {  
  23.             if (curPoint[1] >= startpoint[1]) {  
  24.                 console.log(e.timestamp + '-touch down move')  
  25.             } else {  
  26.                 console.log(e.timestamp + '-touch up move')  
  27.             }  
  28.         }  
  29.     } else {  
  30.         if (Math.abs(curPoint[0] - startpoint[0] >= Math.abs(curPoint[1] - startpoint[1]))) {  
  31.             console.log(e.timestamp + '-touch right move')  
  32.             that.setData({  
  33.                 dellStyle: "modList"  
  34.             })  
  35.         } else {  
  36.             if (curPoint[1] >= startpoint[1]) {  
  37.                 console.log(e.timestamp + '-touch down move')  
  38.             } else {  
  39.                 console.log(e.timestamp + '-touch up move')  
  40.             }  
  41.         }  
  42.     }  
  43. },  


 

 

5.多點(diǎn)觸控

由于模擬器尚不支持多點(diǎn)觸控,內(nèi)測(cè)開(kāi)放后,繼續(xù)補(bǔ)充。

 

實(shí)際使用中遇到的坑;

本身使用的是 scroll-view 進(jìn)行下拉刷新事件,想在頁(yè)面list里面進(jìn)行左滑動(dòng)喚醒刪除菜單,實(shí)際上寫(xiě)出來(lái)會(huì)沖突導(dǎo)致 scroll事件無(wú)法觸發(fā),默認(rèn)滑動(dòng)事件了。

長(zhǎng)按:touchstart → longtap → touchend → tap

本身list綁定了單擊事件點(diǎn)擊進(jìn)入詳情頁(yè),準(zhǔn)備增加一個(gè)長(zhǎng)按喚醒操作菜單,實(shí)際體驗(yàn)并不好,長(zhǎng)按之后手指不能直接離開(kāi)屏幕那樣會(huì)觸發(fā)點(diǎn)擊事件,需要輕微移動(dòng)一下離開(kāi)屏幕。

解決思路

前端綁定三個(gè)事件

 

[html] view plain copy
 
  1. bindtouchstart="mytouchstart" bindtouchend="mytouchend"  bindtap="bindtap"  


js處理通過(guò)點(diǎn)擊開(kāi)始結(jié)束判斷點(diǎn)擊屏幕時(shí)間,進(jìn)行不同的業(yè)務(wù)觸發(fā)

 

[html] view plain copy
 
  1. bindtap: function (e) {  
  2.     var that = this;  
  3.     //觸摸時(shí)間距離頁(yè)面打開(kāi)的毫秒數(shù)    
  4.     var touchTime = that.data.touch_end - that.data.touch_start  
  5.     console.log(touchTime)  
  6.     //如果按下時(shí)間大于350為長(zhǎng)按    
  7.     if (touchTime > 350) {  
  8.         wx.showModal({  
  9.             title: '提示',  
  10.             content: '這是一個(gè)模態(tài)彈窗',  
  11.             success: function (res) {  
  12.                 if (res.confirm) {  
  13.                     console.log('用戶點(diǎn)擊確定')  
  14.                 } else if (res.cancel) {  
  15.                     console.log('用戶點(diǎn)擊取消')  
  16.                 }  
  17.             }  
  18.         })  
  19.     } else {  
  20.         var id = e.currentTarget.dataset.id;  
  21.         wx.navigateTo({  
  22.             url: '../detail/detail?id=' + id  
  23.         })  
  24.     }  
  25. },  
  26. //按下事件開(kāi)始    
  27. mytouchstart: function (e) {  
  28.     let that = this;  
  29.     that.setData({  
  30.         touch_start: e.timeStamp  
  31.     })  
  32.     console.log(e.timeStamp + '- touch-start')  
  33. },  
  34. //按下事件結(jié)束    
  35. mytouchend: function (e) {  
  36.     let that = this;  
  37.     that.setData({  
  38.         touch_end: e.timeStamp  
  39.     })  
  40.     console.log(e.timeStamp + '- touch-end')  
  41. },  
  42.  

 

大云網(wǎng)官方微信售電那點(diǎn)事兒

責(zé)任編輯:售電衡衡

免責(zé)聲明:本文僅代表作者個(gè)人觀點(diǎn),與本站無(wú)關(guān)。其原創(chuàng)性以及文中陳述文字和內(nèi)容未經(jīng)本站證實(shí),對(duì)本文以及其中全部或者部分內(nèi)容、文字的真實(shí)性、完整性、及時(shí)性本站不作任何保證或承諾,請(qǐng)讀者僅作參考,并請(qǐng)自行核實(shí)相關(guān)內(nèi)容。
我要收藏
個(gè)贊
?