溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

微信小程序手勢(shì)操作之單觸摸點(diǎn)與多觸摸點(diǎn)的示例分析

發(fā)布時(shí)間:2021-07-05 10:20:12 來(lái)源:億速云 閱讀:534 作者:小新 欄目:web開(kāi)發(fā)

這篇文章給大家分享的是有關(guān)微信小程序手勢(shì)操作之單觸摸點(diǎn)與多觸摸點(diǎn)的示例分析的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

Demo

為了研究小程序是否支持多手指,需要使用touchstart,touchmove,touchend

// index.wxml
<view id="gestureView" bindtouchstart="touchstartFn" bindtouchmove="touchmoveFn" bindtouchend="touchendFn" >
</view>
//index.js
touchstartFn: function(event){
  console.log(event);
 },
 touchmoveFn: function(event){
  console.log(event);
  // console.log("move: PageX:"+ event.changedTouches[0].pageX);
 },
 touchendFn: function(event){
  console.log(event);
  // console.log("move: PageX:"+ event.changedTouches[0].pageX);
 }

單觸摸點(diǎn),多觸摸點(diǎn)

官方文檔:changedTouches

changedTouches 數(shù)據(jù)格式同 touches。 表示有變化的觸摸點(diǎn),如從無(wú)變有(touchstart),位置變化(touchmove),從有變無(wú)(touchend、touchcancel)。

"changedTouches":[{ 
"identifier":0, "pageX":53, "pageY":14, "clientX":53, "clientY":14
}]

真機(jī)效果

實(shí)現(xiàn)以上Demo后模擬器是無(wú)法看到多觸摸點(diǎn)的數(shù)據(jù)的,所以你需要真機(jī)來(lái)測(cè)試。

看下真機(jī)的log信息

微信小程序手勢(shì)操作之單觸摸點(diǎn)與多觸摸點(diǎn)的示例分析

在changedTouches中按順序保存觸摸點(diǎn)的數(shù)據(jù),所以小程序本身支持多觸摸點(diǎn)的手勢(shì)

結(jié)論

設(shè)想: 既然小程序的手勢(shì)是支持多觸摸,而且可以獲取到相關(guān)的路徑,那么相關(guān)路徑計(jì)算也是可行的。

場(chǎng)景: 多觸摸交互效果,手指繪制等

觸摸點(diǎn)數(shù)據(jù)保存

為了能夠來(lái)分析觸摸點(diǎn)的路徑,最起碼是簡(jiǎn)單的手勢(shì),如左滑、右滑、上滑、下滑,我們需要保存起路徑的所有數(shù)據(jù)。

觸摸事件

觸摸觸發(fā)事件分為"touchstart", "touchmove", "touchend","touchcancel"四個(gè)

詳見(jiàn):https://mp.weixin.qq.com/debug/wxadoc/dev/framework/view/wxml/event.html20

存儲(chǔ)數(shù)據(jù)

var _wxChanges = [];
var _wxGestureDone = false;
const _wxGestureStatus = ["touchstart", "touchmove", "touchend","touchcancel"];
// 收集路徑
function g(e){
  if(e.type === "touchstart"){
    _wxChanges = [];
    _wxGestureDone = false;
  }
  if(!_wxGestureDone){
    _wxChanges.push(e);
    if(e.type === "touchend"){
      _wxGestureDone = true; 
    }else if(e.type === "touchcancel"){
      _wxChanges = [];
      _wxGestureDone = true; 
    }
  }
}

感謝各位的閱讀!關(guān)于“微信小程序手勢(shì)操作之單觸摸點(diǎn)與多觸摸點(diǎn)的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI