简体   繁体   English

tizen可穿戴应用是否支持Longpress?

[英]Whether Longpress supported in tizen wearable app?

I used mousedown and mouseup events to identify long click.我使用 mousedown 和 mouseup 事件来识别长按。 But that is not working in Tizen emulator for me.但这对我来说在 Tizen 模拟器中不起作用。 But the same code is working fine in browsers.但是相同的代码在浏览器中运行良好。 Tau provided by tizen supports only swipe. tizen 提供的 Tau 只支持滑动。

The code below works fine in browsers:下面的代码在浏览器中工作正常:

 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也提到了这个小提琴

But nothing works in tizen wearable emulator.但是在 tizen 可穿戴模拟器中没有任何作用。 So any other suggestion I can try??那么我可以尝试其他任何建议吗?

You can use 'touchstart' and 'touchend' instead of 'mousedown' and 'mouseup'.您可以使用“touchstart”和“touchend”代替“mousedown”和“mouseup”。

I tested below code now with Basic Web Template.我现在使用基本 Web 模板测试了以下代码。 It working well!它运作良好! :) :)

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);
});

}; };

The accepted answer causes long-press events to trigger in some other situations, like when the user was actually scrolling the view for long enough.接受的答案会导致长按事件在其他一些情况下触发,例如当用户实际滚动视图足够长的时间时。 Also any regular handler (like regular tap) would still fire on a long-press.此外,任何常规处理程序(如常规点击)仍会在长按时触发。 This version fixes those issues.此版本修复了这些问题。 Also it will produce the usual feedback (haptic and audio, as configured in the device settings) for a long-press.它还将为长按产生通常的反馈(触觉和音频,如设备设置中所配置)。

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);
});

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

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