te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>React Native Allow a WebView within an Animated.ScrollView to scroll using gesture detector function - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

React Native Allow a WebView within an Animated.ScrollView to scroll using gesture detector function - Stack Overflow

programmeradmin3浏览0评论

Okay, this one's kind of weird, I know.

I'm trying to get a WebView to scroll within an Animated.ScrollView. Everything works, except that the WebView will only scroll after I move the Animated.View a certain distance. IMPORTANT NOTE: Animated.View is != to Animated.ScrollView. I don't mean to get confusing, but I need both in order for the page to work.

The return() code looks like this, I cut out some of the unnecessary code:

return(
<PanGestureHandler onGestureEvent={onGestureEvent2} >
    <Animated.View style={[ styles.bottomHalf, animatedStyle2 ]}>
       <Animated.ScrollView
      nestedScrollEnabled={true}
      scrollEnabled={true}
      scrollEventThrottle={16}
      contentContainerStyle={{ paddingTop: 25, paddingBottom: 50 }}
    >
<GestureDetector gesture={panGesture}>
<View style={styles.container3}>
      <WebView 
    ref={webviewRef}
    source={{ uri: youtubeUrl }}
    style={{ flex: 1 }}
    onScroll={(event) => {
      if(!isVerticalScroll.value) {
        runOnJS(()=>{
          if( webviewRef.current ){
            webviewRef.current.scrollTo({
              x: 0,
              y: event.nativeEvent.contentOffset.y,
              animated: false,
            })
          }
        })
      }
    }
    }
    scrollEnabled={isVerticalScroll.value}
    allowsFullscreenVideo={true}
    />
</View>
</GestureDetector>   
</Animated.ScrollView>
    </Animated.View>
   </PanGestureHandler>
)

Note that in the above code I did not include the function that handles animatedStyle2 within the Animated.View, this is b/c it works just fine. The imports and setting states, as well as the gesture handler panGesture function look like this:

import { PanGestureHandler,Gesture, GestureDetector } from 'react-native-gesture-handler'
import Animated, { useSharedValue, runOnJS, useAnimatedStyle, withTiming, withSpring } from 'react-native-reanimated'

import { WebView } from 'react-native-webview'

const isVerticalScroll = useSharedValue(null)
const webviewRef = useRef(null)

const panGesture = Gesture.Pan().onStart(()=>{
        console.log('Gesture Started');
        isVerticalScroll.value = false
        })
        .onUpdate((event) => {
            if (Math.abs(event.velocityY) > Math.abs(event.velocityX)) {
                isVerticalScroll.value = true
            } else {
                isVerticalScroll.value = false
            }
        })
        .onEnd(() => {
            console.log('Gesture Ended');
        });

The console.log() renders the 'Gesture Started' and 'Gesture Ended' messages just fine. The only problem is the WebView will not immediately scroll within its ScrollView like normal.

What I want: WebView within the Animated.ScrollView to scroll immediately, without me having to move the Animated.View at all.

What I'm getting: WebView will only scroll if I move the Animated.View a certain distance.

Any help would be appreciated. I tried everything I could think of.

Okay, this one's kind of weird, I know.

I'm trying to get a WebView to scroll within an Animated.ScrollView. Everything works, except that the WebView will only scroll after I move the Animated.View a certain distance. IMPORTANT NOTE: Animated.View is != to Animated.ScrollView. I don't mean to get confusing, but I need both in order for the page to work.

The return() code looks like this, I cut out some of the unnecessary code:

return(
<PanGestureHandler onGestureEvent={onGestureEvent2} >
    <Animated.View style={[ styles.bottomHalf, animatedStyle2 ]}>
       <Animated.ScrollView
      nestedScrollEnabled={true}
      scrollEnabled={true}
      scrollEventThrottle={16}
      contentContainerStyle={{ paddingTop: 25, paddingBottom: 50 }}
    >
<GestureDetector gesture={panGesture}>
<View style={styles.container3}>
      <WebView 
    ref={webviewRef}
    source={{ uri: youtubeUrl }}
    style={{ flex: 1 }}
    onScroll={(event) => {
      if(!isVerticalScroll.value) {
        runOnJS(()=>{
          if( webviewRef.current ){
            webviewRef.current.scrollTo({
              x: 0,
              y: event.nativeEvent.contentOffset.y,
              animated: false,
            })
          }
        })
      }
    }
    }
    scrollEnabled={isVerticalScroll.value}
    allowsFullscreenVideo={true}
    />
</View>
</GestureDetector>   
</Animated.ScrollView>
    </Animated.View>
   </PanGestureHandler>
)

Note that in the above code I did not include the function that handles animatedStyle2 within the Animated.View, this is b/c it works just fine. The imports and setting states, as well as the gesture handler panGesture function look like this:

import { PanGestureHandler,Gesture, GestureDetector } from 'react-native-gesture-handler'
import Animated, { useSharedValue, runOnJS, useAnimatedStyle, withTiming, withSpring } from 'react-native-reanimated'

import { WebView } from 'react-native-webview'

const isVerticalScroll = useSharedValue(null)
const webviewRef = useRef(null)

const panGesture = Gesture.Pan().onStart(()=>{
        console.log('Gesture Started');
        isVerticalScroll.value = false
        })
        .onUpdate((event) => {
            if (Math.abs(event.velocityY) > Math.abs(event.velocityX)) {
                isVerticalScroll.value = true
            } else {
                isVerticalScroll.value = false
            }
        })
        .onEnd(() => {
            console.log('Gesture Ended');
        });

The console.log() renders the 'Gesture Started' and 'Gesture Ended' messages just fine. The only problem is the WebView will not immediately scroll within its ScrollView like normal.

What I want: WebView within the Animated.ScrollView to scroll immediately, without me having to move the Animated.View at all.

What I'm getting: WebView will only scroll if I move the Animated.View a certain distance.

Any help would be appreciated. I tried everything I could think of.

Share Improve this question asked Feb 18 at 0:19 jedihomeslicejedihomeslice 477 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Nevermind, guys. I figured it out.

I knew I needed a useEffect somewhere....

const webviewRef = useRef(null);
const translateY = useSharedValue(0);

const panGesture = Gesture.Pan()
    .onUpdate((event) => {
      translateY.value += event.changeY;
    })
    .onEnd(() => {
      // Optionally add momentum or snapping behavior here
    });

    useEffect(() => {
      // Reset translateY when the component mounts or dependencies change
      translateY.value = 0;
    }, []);

And the return....

<Animated.ScrollView
      nestedScrollEnabled={true}
      scrollEnabled={true}
      scrollEventThrottle={16}
      contentContainerStyle={{ paddingTop: 25, paddingBottom: 50 }}
    >
<GestureDetector gesture={panGesture}>
<View style={styles.container3}>
      <WebView 
    ref={webviewRef}
    source={{ uri: youtubeUrl }}
    style={{ flex: 1 }}
    allowsFullscreenVideo={true}
    nestedScrollEnabled 
    />
</View>
</GestureDetector>   
</Animated.ScrollView>

What a pain in the butt that was to figure out!

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论