简体   繁体   English

使用jQuery在Android上确定长按(长按,点按保持)

[英]Determining Long Tap (Long Press, Tap Hold) on Android with jQuery

I've been able to successfully play with the touchstart, touchmove, and touchend events on Android using jQuery and an HTML page. 我已经能够使用jQuery和HTML页面在Android上成功地使用touchstart,touchmove和touchend事件。 Now I'm trying to see what the trick is to determine a long tap event, where one taps and holds for 3 seconds. 现在我想看看诀窍是什么,以确定一个长敲击事件,其中一个点击并保持3秒。 I can't seem to figure this out yet. 我似乎无法解决这个问题。 I'm wanting to this purely in jQuery without Sencha Touch, JQTouch, jQMobile, etc. 我想在没有Sencha Touch,JQTouch,jQMobile等的jQuery中纯粹这样做。

I like the concept of jQTouch, although it doesn't provide me a whole lot and some of my code breaks with it. 我喜欢jQTouch的概念,虽然它并没有为我提供很多东西,但我的一些代码却打破了它。 With Sencha Touch, I'm not a fan of moving away from jQuery into Ext.js and some new way of doing Javascript abstraction, especially when jQuery is so capable. 使用Sencha Touch,我不喜欢将jQuery转移到Ext.js以及一些新的Javascript抽象方式,尤其是当jQuery非常强大时。 So, I want to figure this out with jQuery alone. 所以,我想单独使用jQuery来解决这个问题。 I've been able to do many jQTouch and Sencha Touch things on my own using jQuery. 我已经能够使用jQuery自己做很多jQTouch和Sencha Touch的事情了。 And jQMobile is still too beta and not directed enough to the Android yet. 并且jQMobile仍处于测试阶段,尚未对Android进行足够的指导。

Timers not used but will only fire after user releases his finger after a long tap. 定时器未使用但只会在用户松开长按后松开手指。

var startTime, endTime;
var gbMove = false;

window.addEventListener('touchstart',function(event) {
    startTime = new Date().getTime(); 
    gbMove = false;        
},false);

window.addEventListener('touchmove',function(event) {
  gbMove = true;
},false);

window.addEventListener('touchend',function(event) {
    endTime = new Date().getTime();
    if(!gbMove && (endTime-startTime)/1000 > 2)
        alert('tap hold event');      
},false);
var gnStartTime = 0;
var gbMove = false;
var gbStillTouching = false;

function checkTapHold(nID) {
  if ((!gbMove) && (gbStillTouching) && (gnStartTime == nID)) {
    gnStartTime = 0;
    gbMove = false; 
    alert('tap hold event');    
  }
}

window.addEventListener('touchstart',function(event) {
  gbMove = false;
  gbStillTouching = true;
  gnStartTime = Number(new Date());
  setTimeout('checkTapHold(' + gnStartTime + ');',2000);
},false);

window.addEventListener('touchmove',function(event) {
  gbMove = true;
},false);

window.addEventListener('touchend',function(event) {
  gbStillTouching = false;
},false);

Why not just use a js timer? 为什么不使用js计时器? I did something like: 我做了类似的事情:

i=0;
$drink = document.getElementById('drink');
$drink.addEventListener('touchstart',function(event){
    $('#drink .mainBtnText').text('HOLD'); //mostly for debugging
    t = window.setInterval(function(event){
            i+=.5;
            if (i>=1){
                alert('taphold');
                window.clearInterval(t);
                i=0;
            }
        },500);
});
$drink.addEventListener('touchend',function(event){
    $('#drink .mainBtnText').text('Drink');
    i=0;
    window.clearInterval(t);
});

And I've had absolutely no trouble with it thus far...admittedly, I haven't done incredibly extensive device testing, but it's worked on my desktop (with mousedown/mouseup) as well as an HTC Droid Eris (Android 1.6) and my Droid RAZR (Android 2.3)... 到目前为止,我已经完全没有遇到任何问题...不可否认,我没有做过非常广泛的设备测试,但它在我的桌面上工作(使用mousedown / mouseup)以及HTC Droid Eris(Android 1.6)和我的Droid RAZR(Android 2.3)......

I did something like this: 我做了这样的事情:

var touchCounter;

window.addEventListener('touchstart', function () {
  var count = 0;
  touchCounter = setInterval(function () {
    count++;
    if (count == 10) alert('touch lasted 10 seconds');
  }, 1000);
});

window.addEventListener('touchend', function () {
  clearInterval(touchCounter);
  touchCounter = null;
});

Although it is not jQuery but I've done it this way in my Android app: 虽然它不是jQuery,但我已经在我的Android应用程序中这样做了:

  1. registered events listeners: 注册事件听众:

     var touchStartTimeStamp = 0; var touchEndTimeStamp = 0; window.addEventListener('touchstart', onTouchStart,false); window.addEventListener('touchend', onTouchEnd,false); 
  2. added functions: 增加的功能:

     var timer; function onTouchStart(e) { touchStartTimeStamp = e.timeStamp; } function onTouchEnd(e) { touchEndTimeStamp = e.timeStamp; console.log(touchEndTimeStamp - touchStartTimeStamp);// in miliseconds } 
  3. checked time difference and did my stuff 检查时差,做了我的东西

I hope this will help. 我希望这将有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM