禁用触摸并按住后的点击事件

发布于 2024-12-04 02:18:19 字数 1463 浏览 5 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

探春 2024-12-11 02:18:19

我以前也遇到过这个问题。我使用的解决方案不是很漂亮,但这是我发现的在触摸并按住后抑制触摸事件的唯一有效方法。

我能找到的唯一可行的解​​决方案是在全局命名空间中创建一个 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 your setTimeout function, change the value of the bool to true 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 the onClick 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文