2016-08-03 8 views
5

私はReact Native via PanResponderで長いプレスを処理しようとしています。まともな検索の後、私はそれを "正しい方法"にする方法を見つけることができませんでしたので、私はここで尋ねています。このアイデアは、画面上の長い押し(クリック)が検出されたときにコードを実行することです。 私はこのような何かに出ている:PanResponderロングプレスイベントの処理方法は?

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

が、これは長押しを処理するための正しい方法ですか良い方法はありますか?

+0

'this.MyExecutableFunctionでテストされていない()' 'this.MyExecutableFunction'と'てclearTimeout(myVarに)なければならない 'もhandlePanResponderTerminate''で実行されなければなりませんプレスが終了した後、アプリケーションは長押しとしてカウントしないことを確認してください。 –

答えて

7

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

'onShouldBlockNativeResponder'をAndroidデバイスの' PanResponder'に 'false'を返すように設定してください。そうでなければ、' PanResponder'はジェスチャーをハイジャックし、通常のスクロールを許可しません。 –

0

私はScrollView内Carouselを持っている、と私は、ユーザーがCarouselの項目に押された場所を知りたいと思いました。私は@Alexander Netsovに感謝しました。

this._panResponder = PanResponder.create({ 
    onStartShouldSetPanResponder:() => true, 
    onMoveShouldSetPanResponder:() => false, 
    onPanResponderGrant: (e, gestureState) => { 
    this.onLongPressTimeout = setTimeout(() => { 
     console.log("ON LONG PRESS", gestureState); 
    }, LONG_PRESS_DELAY); 
    }, 
    onPanResponderRelease:() => { 
    clearTimeout(this.onLongPressTimeout); 
    }, 
    onPanResponderTerminate:() => { 
    clearTimeout(this.onLongPressTimeout); 
    }, 
    onShouldBlockNativeResponder:() => false, 
    onPanResponderTerminationRequest:() => true 
}); 

CarouselPanResponder水平垂直ScrollView、すべては、Android上で完璧に取り組んでいます。

注:そのIOSの

関連する問題