禁用触摸并按住后的点击事件
我正在 appcelerator 中开发一个 iOS 应用程序,我得到了一张与用户的表。 当我点击用户时,它会打开个人资料,但我也希望用户只需点击并按住 2 秒钟即可复制名称。
这两个事件单独工作正常,但现在点击并按住后单击事件会触发。如何防止点击按住后触发点击事件?
// Set the timeout
var holdTime = 500, timeout;
// Create the table touch start event listener
table.addEventListener('touchstart', function(e) {
// Set the selected user id
var itemValue = e.row.value_full;
// Define the function
timeout = setTimeout(function(e) {
// Create the fade out animation
var fadeOut = Titanium.UI.createAnimation({
curve: Ti.UI.ANIMATION_CURVE_EASE_IN_OUT,
opacity: 0,
duration: 1000
});
// Create the loading screen
var copied = UI_messages.showFlash({label : 'Copied!'});
// Add the loading screen
win.add(copied);
// Save value to clipboard
Titanium.UI.Clipboard.setText(itemValue);
// Fade the message out
copied.animate(fadeOut);
}, holdTime);
});
// Create the event listener for touch move
table.addEventListener('touchmove', function() {
// Clear the timeout
clearTimeout(timeout);
});
// Create the event listener for touch move
table.addEventListener('touchend', function(e) {
// Clear the timeout
clearTimeout(timeout);
});
I am developing an app iOS app in appcelerator and I got a table with user.
When I click on the user it opens the profile but I also want the user to be able to copy the name just by tap and hold for 2 seconds.
These two event works fine separately but right now after tap and hold the click event fires to. How can I prevent the click event from firing after tap hold?
// Set the timeout
var holdTime = 500, timeout;
// Create the table touch start event listener
table.addEventListener('touchstart', function(e) {
// Set the selected user id
var itemValue = e.row.value_full;
// Define the function
timeout = setTimeout(function(e) {
// Create the fade out animation
var fadeOut = Titanium.UI.createAnimation({
curve: Ti.UI.ANIMATION_CURVE_EASE_IN_OUT,
opacity: 0,
duration: 1000
});
// Create the loading screen
var copied = UI_messages.showFlash({label : 'Copied!'});
// Add the loading screen
win.add(copied);
// Save value to clipboard
Titanium.UI.Clipboard.setText(itemValue);
// Fade the message out
copied.animate(fadeOut);
}, holdTime);
});
// Create the event listener for touch move
table.addEventListener('touchmove', function() {
// Clear the timeout
clearTimeout(timeout);
});
// Create the event listener for touch move
table.addEventListener('touchend', function(e) {
// Clear the timeout
clearTimeout(timeout);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我以前也遇到过这个问题。我使用的解决方案不是很漂亮,但这是我发现的在触摸并按住后抑制触摸事件的唯一有效方法。
我能找到的唯一可行的解决方案是在全局命名空间中创建一个
bool
变量。在setTimeout
函数中,将bool
的值更改为true
以指示发生了触摸并按住操作。在该行的
onClick
事件中,首先检查全局变量,看看您是否已经创建了触摸并按住事件 - 如果有,只需从onClick< /代码> 事件。当发生触摸并按住时,这将有效地禁用您的点击事件。
请记住在触摸并按住功能结束后将全局变量设置为
false
。I've run into this problem before as well. The solution I used isn't very pretty, but it's the only effective way that I've found to suppress a touch event after a touch-and-hold.
The only working solution that I could find was to create a
bool
variable in a global namespace. In yoursetTimeout
function, change the value of thebool
totrue
to indicate that a touch-and-hold has occurred.In the
onClick
event event for the row, check the global variable first to see if you've already created a touch-and-hold event - if you have, just return from theonClick
event. This will effectively disable your click event when a touch-and-hold occurs.Remember to set the global variable to
false
after the touch-and-hold function ends.