windows phone和android,ios的touch事件兼容


  

1.開發背景

 

  最近用html5寫了個小游戲,中間踩過無數坑,有很多甚至百度都百度不到答案,可見html5還真是不成熟,兼容性的復雜度比ie6有過之而無不及,性能那個渣簡直無力吐槽。。

  好了,吐槽結束,雖然有這么多的缺點,但是由於其良好的跨平台前景以及極低的學習成本,再加上優秀的框架,我最終還是選擇了用html5來開發這個小游戲,而且是小游戲,所以就沒有用什么游戲開發框架了,只是自己簡單的封裝了一個,因此所有的bug都被我走了一遍。。正當我調試完所有的android上的bug之后,心想自己的努力不能白費啊,跨平台呢?上wp看看,結果,。。發現居然沒有結果了。。自己平時用wp手機為主,android只是開發機( 淘寶上買的屌絲機).然后又是百度又是bing,最終發現原來ie10種中的觸摸事件和android、ios不一樣,貌似ie10是MSPointerMove之類,ie11是pointermove(居然同一系列還沒事改名,這無疑增大了兼容負擔。。)看到此處lz還挺盲目樂觀,至少還有一大批的兼容框架吧。。框架確實有,不過基本上都是集成的重型webapp框架,比如zepto的touch.js, 在wp8手機上配合jquery失敗。我一個小游戲實在沒有必要啊,否則還不如直接學個游戲引擎(lz太懶,看到那么繁復的api又沒有良好的開發工具(webstorm雖然好,畢竟是破解的。。),而且自己也不是專門搞前端的,只是興趣愛好,所以每次想學就又半途而廢了。。)

 

  最終,終於讓我找到了pointer.js,雖然他的api方式是微軟版本的(采用少數派,有點不爽),但是能湊合着用也算了,不過在wp8手機上測試失敗,事實上ie10也測試失敗,什么堆棧溢出,自己改了改源碼,不報錯了,但是觸摸也沒有反應了,於是又放棄了。。

 

  最后lz鼓起勇氣,決定自己寫一個小的封裝,但是在寫的過程中又是坑無數。。因為lz新系統沒有裝wp8的sdk(以前裝過2012版的,貌似和校園網客戶端有沖突,於是就只敢裝虛擬機里了),所以只能用原始的js alert()調試,結果發現,那些網上的博客,框架,都采用的MSPointer[Down|Up]在滑動手勢中是不會觸發的,只有在點擊才會觸發。。我了個去啊,這算什么回事,難道真的得放棄wp版本嗎?放棄了wp平台,這跨平台也跨的太坑爹了吧。。而且在滑動事件中MSPointerMove也只會觸發兩次,估計只是第一個點和最后一個點兩次。就在lz萬分懊惱,快要被html5搞得神經錯亂之際(其實在開發過程中我總是在猶豫要不要用原生的java開發android版本算了,性能絕對能好上一個數量級,現在畫面掉幀嚴重。。),忽然靈光一閃,既然down,up不觸發那么over和out呢?果斷換成MSPointer[over|out],居然成功了!!然后自己一頓簡單封裝,只支持單點觸控,多點觸控什么的都沒考慮了,電腦的mouse事件雖有考慮,不過基本無效,以后有時間再完善吧,不過現在暫時夠用了。現在把思路發出來,如果大家有需要的二次開發什么的也方便。。

 

代碼寫得可能不夠優雅,實在是這個需要兼容的太混亂,加之水平有限,所以還請大家海涵,不喜勿噴。。。

 

2.上源碼

/** * 兼容ie10,11和android、ios的觸摸事件,只需要和android,ios一樣使用函數就可以了, */ var TouchFix = {}; (function() { var MSPointerType={ start:"MSPointerOver", move:"MSPointerMove", end:"MSPointerOut" }, pointerType={ start:"pointerover", move:"pointermove", end:"pointerout" }, touchType={ start:"touchstart", move:"touchmove", end:"touchend" }, mouseType={ start:"mousedown", move:"mousemove", end:"mouseup", out:"mouseout" }; function isTouch() { return typeof window.ontouchstart !== "undefined"; } function isMSPointer() { return window.navigator.msPointerEnabled; } function isPointer() { return window.navigator.pointerEnabled; } function bindStart(el,cb) { el.addEventListener(pointerType.start, function (e) { pointerHandler(e,cb); }); el.addEventListener(MSPointerType.start, function (e) { MSPointerHandler(e,cb); }); el.addEventListener(touchType.start, function (e) { touchHandler(e,cb); }); if (!isTouch() && !isMSPointer() && !isPointer()) { el.addEventListener(mouseType.start, function (e) { mouseHandler(e,cb); }); } } function bindMove(el,cb) { el.addEventListener(pointerType.move, function (e) { pointerHandler(e,cb); cb(e); }); el.addEventListener(MSPointerType.move, function (e) { MSPointerHandler(e,cb); cb(e); }); el.addEventListener(touchType.move, function (e) { touchHandler(e,cb); }); if (!isTouch() && !isMSPointer() && !isPointer()) { el.addEventListener(mouseType.move, function (e) { mouseHandler(e,cb); }); } } function bindEnd(el,cb) { el.addEventListener(pointerType.end, function (e) { pointerHandler(e,cb); }); el.addEventListener(MSPointerType.end, function (e) { MSPointerHandler(e,cb); }); el.addEventListener(touchType.end, function (e) { touchHandler(e,cb); }); if (!isTouch() && !isMSPointer() && !isPointer()) { el.addEventListener(mouseType.end, function (e) { mouseHandler(e,cb); }); el.addEventListener(mouseType.out, function (e) { mouseHandler(e,cb); }); } } TouchFix.bind = function(el,type,cb) { switch (type) { case touchType.start: bindStart(el,cb); break; case touchType.move: bindMove(el,cb); break; case touchType.end: bindEnd(el,cb); break; default: break; } } var hasTouchStart=false; function commonHandler (e) { if(e.type===MSPointerType.start ||e.type===pointerType.start ||e.type===mouseType.start){ e.type=touchType.start; }else if(e.type===MSPointerType.move ||e.type===pointerType.move ||e.type===mouseType.move){ e.type=touchType.move; }else if(e.type===MSPointerType.end ||e.type===pointerType.end ||e.type===mouseType.end ||e.type===mouseType.out){ e.type=touchType.end; } e.touches=[]; e.pageX=e.clientX; e.pageY=e.clientX; e.touches[0]=e; } function MSPointerHandler(e,cb) { commonHandler(e); cb(e); } function pointerHandler (e,cb) { commonHandler(e); cb(e); } function touchHandler (e,cb) { cb(e); } function mouseHandler (e,cb) { commonHandler(e); cb(e); } })();
touchfix.js

 

 

 1 TouchFix.bind(element,"touchstart",function(e){
 2     var t=e.touches[0];
 3     var x=t.pageX;
 4     var y=t.pageY;
 5 });
 6 TouchFix.bind(element,"touchmove",function(e){
 7     var t=e.touches[0];
 8     var x=t.pageX;
 9     var y=t.pageY;
10 });
11 TouchFix.bind(element,"touchend",function(e){
12 //在安卓中貌似在這里獲取不到e,只能用move中的最后一個點代替
13     if(!e||!e.touches||e.touches.length===0){
14         return ;
15     }
16     var t=e.touches[0];
17     var x=t.pageX;
18     var y=t.pageY;
19 });

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM