触摸并按住后禁用单击事件

时间:2011-09-11 21:39:03

标签: appcelerator

我正在appcelerator中开发一个应用程序iOS应用程序,我得到了一个用户表。 当我点击用户时,它会打开个人资料,但我也希望用户能够通过点击并按住2秒来复制名称。

这两个事件分别正常工作但现在点击并按住click事件后触发。如何在点击保持后阻止点击事件触发?

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

    });

1 个答案:

答案 0 :(得分:2)

我之前也遇到过这个问题。我使用的解决方案不是很漂亮,但它是我发现在触摸和保持之后抑制触摸事件的唯一有效方式。

我能找到的唯一可行的解​​决方案是在全局命名空间中创建一个bool变量。在setTimeout功能中,将bool的值更改为true,以表示已发生触摸并保持。

在该行的onClick事件事件中,首先检查全局变量,看看您是否已经创建了一个触摸并保持事件 - 如果有,只需从onClick返回事件。这将在发生触摸并保持时有效地禁用您的点击事件。

请记住在触摸保持功能结束后将全局变量设置为false

相关问题