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');
}

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...