【问题标题】:Whether Longpress supported in tizen wearable app?tizen可穿戴应用程序是否支持Longpress?
【发布时间】:2016-06-27 13:57:28
【问题描述】:

我使用 mousedown 和 mouseup 事件来识别长按。但这对我来说在 Tizen 模拟器中不起作用。但是相同的代码在浏览器中运行良好。 tizen 提供的 Tau 只支持滑动。

以下代码在浏览器中运行良好:

var timeOut;
$("button").mouseup(function(event){
     
    clearTimeout(timeOut);
});
$("button").mousedown(function(event){
    
    timeOut = setTimeout(function(){
        
        alert('you hold your mouse more than 2 seconds.!');
      
    },2000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>click me</button>

Referred this fiddle too

但在 tizen 可穿戴仿真器中没有任何作用。那么我可以尝试其他任何建议吗??

【问题讨论】:

    标签: tizen


    【解决方案1】:

    您可以使用“touchstart”和“touchend”来代替“mousedown”和“mouseup”。

    我现在使用基本 Web 模板测试了以下代码。它运作良好! :)

    main.js

    window.onload = function() {
    // TODO:: Do your initialization job
    
    // add eventListener for tizenhwkey
    document.addEventListener('tizenhwkey', function(e) {
        if (e.keyName === "back") {
            try {
                tizen.application.getCurrentApplication().exit();
            } catch (ignore) {}
        }
    });
    
    // Sample code
    var mainPage = document.querySelector('#main');
    
    var timeOut;
    var cnt = 0;
    mainPage.addEventListener("touchend", function() {
        var contentText = document.querySelector('#content-text');
        console.log("timer clear!");
        clearTimeout(timeOut);
    });
    
    mainPage.addEventListener("touchstart", function() {
        var contentText = document.querySelector('#content-text');
        console.log("touchstart!");
        timeOut = setTimeout(function(){
            console.log("long!");
            contentText.innerHTML = "Long!" + cnt;
            cnt++;
        },2000);
    });
    

    };

    【讨论】:

      【解决方案2】:

      接受的答案会导致在某些其他情况下触发长按事件,例如当用户实际滚动视图足够长的时间时。此外,任何常规处理程序(如常规水龙头)仍会在长按时触发。这个版本修复了这些问题。它还会为长按产生通常的反馈(触觉和音频,如设备设置中配置的那样)。

      var longpressTimer;
      var wasLongpress = false;
      function endLongpress(e) {
          if (wasLongpress) {
              // Prevent default handling, like taps
              e.preventDefault();
              wasLongpress = false;
          } else if (longpressTimer) {
              clearTimeout(longpressTimer);
              longpressTimer = null;
          }
      }
      
      // Could target any other element, like a specific control.
      var target = document.getElementById('body');
      // Any of these should cancel a long-press, or be canceled if the last touch was the start of a long-press
      target.addEventListener("touchcancel", function(e) {
          endLongpress(e);
      });
      
      target.addEventListener("touchmove", function(e) {
          endLongpress(e);
      });
      
      target.addEventListener("touchend", function(e) {
          endLongpress(e);
      });
      
      target.addEventListener("touchstart", function(e) {
          longpressTimer = setTimeout(function() {
              longpressTimer = null;
              e.preventDefault();
              // So that we know we should prevent default handling
              wasLongpress = true;
              try {
                  tizen.feedback.play('HOLD');
              } catch (err) {
                  //unable to play feedback
              }
              // handle long-press from here
              console.log("long!");
          }, 500);
      });
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-03
        • 1970-01-01
        • 2019-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多