微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 如何处理PanResponder长按事件?

我试图通过PanResponder处理React Native中的长按.经过一番体面的搜索,我找不到如何以“正确的方式”做到这一点,所以我在这里问.这个想法是在检测到屏幕上长按(点击)时执行代码.
我达到了这样的目的:
handlePanResponderGrant(e,gestureState){
    // On the press of the button set a timeout
    myVar = setTimeout(this.MyExecutableFunction(),LONG_PRESS_MIN_DURATION);
}

handlePanResponderRelease(e,gestureState) {
    // Clear the timeout if the press is released earlier than the set duration
    clearTimeout(myVar);
}

这是处理长按的正确方法还是有更好的方法

解决方法

我最终使用setTimeout执行此功能.这是一个代码,它具有检测屏幕的哪个部分被长按(左或右)的功能
handlePanResponderGrant(e,gestureState) {
    console.log('Start of touch');

    this.long_press_timeout = setTimeout(function(){
            if (gestureState.x0 <= width/2 )
            {
                AlertIOS.alert(
                  'Left','Long click on the left side detected',[
                    {text: 'Tru dat'}
                  ]
                );
            }
            else {
                AlertIOS.alert(
                  'Right','So you clicked on the right side?',[
                    {text: 'Indeed'}
                  ]
                );
            }
        },LONG_PRESS_MIN_DURATION);
}
handlePanResponderMove(e,gestureState) {
    clearTimeout(this.long_press_timeout);
}
handlePanResponderRelease(e,gestureState){
    clearTimeout(this.long_press_timeout);
    console.log('Touch released');
}
handlePanResponderEnd(e,gestureState) {
    clearTimeout(this.long_press_timeout);
    console.log('Finger pulled up from the image');
}

原文地址:https://www.jb51.cc/js/158215.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐