如何在React Native React Navigation 5中为标题后退按钮的颜色设置动画?

Kaizen Tamashi

我正在尝试对标题后退按钮的颜色进行动画处理,以从React本机反应导航5的灰色灰色背景与白色箭头图标变为白色背景与黑色箭头图标。

在此处输入图片说明

在此处输入图片说明

我尝试执行以下操作,但是它抛出了 RangeError: Maximum call stack size exceeded.

const yOffset = useRef(new Animated.Value(0)).current;

const backButtonBackgroundColorAnimation = yOffset.interpolate({
        inputRange: [0, 130],
        outputRange: ['rgba(0,0,0,0.4)', 'rgba(0,0,0,0)'], // gray transparent to transparent
        extrapolate: "clamp"
      });

      const backArrowColorAnimation = yOffset.interpolate({
        inputRange: [0, 130],
        outputRange: ['rgb(255,255,255)', 'rgb(0,0,0)'], // white to black
        extrapolate: "clamp"
    });

import {Icon} from 'react-native-elements';

headerLeft: (props) => (                          
              <Animated.View style={{backgroundColor: backButtonOpacity}} >                           
                  <Icon                       
                    name='arrowleft'
                    type='antdesign'
                    color='white'
                    size={24}                            
                    containerStyle={{ backgroundColor:backButtonBackgroundColorAnimation, color:backArrowColorAnimation, borderRadius:500, padding: 5, marginLeft:10}}
                    {...props}
                    onPress={() => {
                        navigation.goBack();
                    }}
                    />
              </Animated.View>                                                       
          )
<Animated.ScrollView        
          onScroll={Animated.event(
            [
              {
                nativeEvent: {
                  contentOffset: {
                    y: yOffset,
                  },
                },
              },
            ],
            { useNativeDriver: false }
          )}
          scrollEventThrottle={16}
        >
丹尼

问题似乎是react-native-elements图标不是动画组件。您可以使用来制作动画

import { Icon } from 'react-native-elements';
import { Animated } from 'react-native';
const AnimatedIcon = Animated.createAnimatedComponent(Icon);

还要对其进行调整,以便使用样式而不是容器样式。

headerLeft: (props) => (
    <Animated.View style={{ opacity: headerOpacity }}>
      <AnimatedIcon
        name="arrowleft"
        type="antdesign"
        color={backArrowColorAnimation}
        size={24}
        style={{
          backgroundColor: backButtonBackgroundColorAnimation,
          borderRadius: 500,
          padding: 5,
          marginLeft: 10,
        }}
        {...props}
        onPress={() => {
          navigation.goBack();
        }}
      />
    </Animated.View>
  ),

有关完整的代码示例,请参见此小吃上的代码https://snack.expo.io/@dannyhw/react-navigation-animated-header2

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在react-native中添加react-navigation?

React Native:设置 React Navigation 的屏幕标题

如何在react-native-navigation中从堆栈完成SplashScreen

React Native Navigation:如何在模式上禁用后退按钮?

如何停止Android硬件后退按钮在react-native的react-navigation中起作用?

如何在React-Native中通过React-Navigation多次传递参数?

在React-Native React Navigation 5中向下滚动时,如何制作从透明到不透明颜色的标题?

如何在 react native navigation v5 中返回另一个堆栈?

如何在React Native Navigation V5的标签栏中隐藏标签?

React Native,React-Navigation

如何在react-native-navigation v2中为所有屏幕添加固定的标题?

React-native-navigation如何在抽屉中添加跳格底部

如何在 react-native-navigation v2 中向我的 sideMenu 添加图标?

如何在 react-native-navigation 中向导航器添加背景图像

如何动态设置推送屏幕的方向-React-Native-Navigation

如何在 React Native 中使用 react-navigation 在具有 goBack 功能的屏幕上添加 customHeaderLeft 按钮?

如何通过React Native Web通过react-navigation设置文档标题?

如何在CustomDrawerContent内使用props.navigation.closeDrawer()?(React Native)(“ @ react-navigation / drawer”:“ ^ 5.11.4”)

React-native 启动画面和 react-navigation

react-native React-Navigation头按钮testID

如何在React Navigation中刷新

如何在React Native中更改导航标题的颜色?

如何在 React Native 中更改完整标题的背景颜色

如何在React Native Navigation v2中更新自定义顶部栏标题组件?

React Native的React-Navigation:在TabNavigator的Tab中添加徽章

如何在 react-native 中制作弹跳按钮动画

React Native中的React Navigation 5问题-HeaderShown:false不会隐藏标题

如何在React Native中安装React Native Maps?

如何在React Native中更改按钮的颜色?