React native 使用动画隐藏标题进行本机反应(转换后留下空白)

React native 使用动画隐藏标题进行本机反应(转换后留下空白),react-native,react-native-reanimated,React Native,React Native Reanimated,我使用动画实现了在React native中滚动时隐藏标题。 代码如下: const y = new Animated.Value(0); const AnimatedWebView = Animated.createAnimatedComponent(WebView); const HEADER_HEIGHT = 60; const {diffClamp} = Animated; function WebPage({route}) { const diffClampY = diffCla

我使用动画实现了在React native中滚动时隐藏标题。 代码如下:

const y = new Animated.Value(0);
const AnimatedWebView = Animated.createAnimatedComponent(WebView);
const HEADER_HEIGHT = 60;
const {diffClamp} = Animated;

function WebPage({route}) {
  const diffClampY = diffClamp(y, 0, HEADER_HEIGHT);
  const translateY = diffClampY.interpolate({
    inputRange: [0, HEADER_HEIGHT],
    outputRange: [0, -HEADER_HEIGHT],
  });
  return (
    <SafeAreaView style={{flex: 1}}>
      <Animated.View
        style={{
          height: HEADER_HEIGHT,
          width: '100%',
          position: 'absolute',
          top: 0,
          left: 0,
          right: 0,
          zIndex: 2,
          backgroundColor: 'lightblue',
          transform: [{translateY: translateY}],
        }}
      />
      <AnimatedWebView
        source={{
          uri: 'https://www.stackoverflow.com',
        }}
        originWhitelist={['*']}
        containerStyle={{paddingTop: 60}}
        bounces={false}
        onScroll={Animated.event([{nativeEvent: {contentOffset: {y: y}}}])}
      />
    </SafeAreaView>
  );
}

const y=新的动画值(0);
const AnimatedWebView=Animated.createAnimatedComponent(WebView);
const HEADER_HEIGHT=60;
const{diffClamp}=动画;
功能网页({route}){
const diffClampY=diffClamp(y,0,收割台高度);
常量translateY=diffClampY.interpolate({
输入范围:[0,收割台高度],
输出范围:[0,-收割台高度],
});
返回(

上面是隐藏标题之前和之后的图像。标题原来所在的位置会留下空白。我应该如何动态更改填充,以便在标题消失时,不会留下空白?我尝试过,没有绝对定位标题,也没有为Webview提供填充顶,但仍然是空白


<谢谢>

当你使用<代码> TrTabeLe>代码时,这意味着你将你的头移离它当前的位置,而不是它所保存的地方。你看到的空白是头的一个锚(打开你的调试器,你会看到你的头组件仍然在原来的位置)。要移动Web视图,只需将Web视图的
translateY
与标题绑定在一起,即可获得完美的动画效果

<AnimatedWebView
    source={{
      uri: 'https://www.stackoverflow.com',
    }}
    originWhitelist={['*']}
    containerStyle={{paddingTop: 60}}
    bounces={false}
    onScroll={Animated.event([{nativeEvent: {contentOffset: {y: y}}}])}
    style={{
        transform: [{translateY: translateY}]
    }}
/>

  • 使您的webview的高度大于其默认高度。因此,当您转换webview时,多余的空间将覆盖您的空白空间。在这种情况下,您必须使用
  • 有一个转换的属性,叫做
    缩放
    。你可以用它来拉伸你的webview,但我不知道它是否会拉伸你的webview的内容

  • 为什么我没有想到一起转换webview。谢谢,伙计!!事实上,如果我翻译webview,问题是它会在webview下面留下空间,因为webview的翻译量。我将如何解决这个问题?我已经更新了我的答案。如果你需要帮助,请随时留下评论。@MinhDao嗨,如果你可以查看我的答案,我也有同样的问题代码。@EricAhn您已经设法解决了这个问题吗?您的
    onScroll={Animated.event([{nativeEvent:{contentOffset:{y:y}}}}])}
    在AnimatedWebView中是如何触发的,您使用的是哪个webView库?