tizen 可穿戴应用程序是否支持 Longpress?
Whether Longpress supported in tizen wearable app?
我使用 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>
但在 tizen 可穿戴模拟器中没有任何效果。那么我可以尝试其他建议吗??
您可以使用 '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);
});
};
接受的答案会导致长按事件在某些其他情况下触发,例如当用户实际滚动视图足够长的时间时。此外,任何常规处理程序(如常规点击)仍会在长按时触发。这个版本修复了这些问题。它还会为长按产生通常的反馈(触觉和音频,在设备设置中配置)。
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);
});
我使用 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>
但在 tizen 可穿戴模拟器中没有任何效果。那么我可以尝试其他建议吗??
您可以使用 '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);
});
};
接受的答案会导致长按事件在某些其他情况下触发,例如当用户实际滚动视图足够长的时间时。此外,任何常规处理程序(如常规点击)仍会在长按时触发。这个版本修复了这些问题。它还会为长按产生通常的反馈(触觉和音频,在设备设置中配置)。
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);
});