为React-Navigation bottom tabbar 添加动效

想要实现的效果:
图片效果
动画我们用lottie-react-native来实现:github项目地址
lottie-react-native的使用方法不赘述了,具体可以参考官网文档
这里主要说一下React-Navigation createBottomTabNavigator中每个StackNavigator里tabBarIcon的使用,主要是这里有个坑,也就是tabBarIcon里的focused有问题,每次点击回调均为
true,false打印两次

    tabBarIcon: ({ focused, tintColor }) => {
	  console.log('focused=====>',focused)
      return <TabBarIcon focused={focused} tintColor={tintColor} index={3} />
    }

我们来查看一下源码:

1.路径:node_modules\react-navigation\src\react-navigation.js 
<--line:116-118-->
  get createBottomTabNavigator() {
    return require('react-navigation-tabs').createBottomTabNavigator;
  },
2.路径:node_modules\react-navigation-tabs\src\navigators\createBottomTabNavigator.js
<--line:131-->
this._renderTabBar() 找到BottomTabBar
3.路径:node_modules\react-navigation-tabs\src\views\BottomTabBar.js
<--line:365-->
this._renderIcon(scene)
找到scene的定义            const scene = { route, focused };
打印log 查看focused的值

到这里我们发现focused的值是正确打印的,那么是哪里出问题了呢,我们继续往下找:

在_renderIcon 查看返回的组件CrossFadeIcon
路径:node_modules\react-navigation-tabs\src\views\CrossFadeIcon.js
然后我们发现在render方法里面有两行注释:
    // We render the icon twice at the same position on top of each other:
    // active and inactive one, so we can fade between them.
大概意思就是这个组件以绝对布局的方式返回了两个icon,一个代表激活也就是选中的icon,一个代表未激活也就是未选中的icon
而且我们查看源码:
  render() {
    const {
      route,
      activeOpacity,
      inactiveOpacity,
      activeTintColor,
      inactiveTintColor,
      renderIcon,
      horizontal,
      style,
    } = this.props;

    // We render the icon twice at the same position on top of each other:
    // active and inactive one, so we can fade between them.
    return (
      <View style={style}>
        <Animated.View style={[styles.icon, { opacity: activeOpacity }]}>
          {renderIcon({
            route,
            focused: true,
            horizontal,
            tintColor: activeTintColor,
          })}
        </Animated.View>
        <Animated.View style={[styles.icon, { opacity: inactiveOpacity }]}>
          {renderIcon({
            route,
            focused: false,
            horizontal,
            tintColor: inactiveTintColor,
          })}
        </Animated.View>
      </View>
    );
  }
  没错,renderIcon props返回的正是我们发现问题的地方-focused返回了一个true和一个false

发现问题我们就好办了,注释掉一个icon,然后将上一个组件的focused字段传过来,搞定
修改之后的代码:

render() {
    const {
      route,
      activeOpacity,
      inactiveOpacity,
      activeTintColor,
      inactiveTintColor,
      renderIcon,
      horizontal,
      style,
      focused
    } = this.props;

    // We render the icon twice at the same position on top of each other:
    // active and inactive one, so we can fade between them.
    return (
      <View style={style}>
        <Animated.View style={[styles.icon]}>
          {renderIcon({
            route,
            focused: focused,
            horizontal,
            tintColor: focused ? activeTintColor : inactiveTintColor,
          })}
        </Animated.View>
        {/* <Animated.View style={[styles.icon, { opacity: inactiveOpacity }]}>
          {renderIcon({
            route,
            focused: false,
            horizontal,
            tintColor: inactiveTintColor,
          })}
        </Animated.View> */}
      </View>
    );
  }

当然上一个组件也需要传focused={focused}过来
修改之后我们在最开始的地方tabBarIcon里面的focused参数就正常了
有了正确的focused之后就好办了
在自定义的icon组件中传过去focused字段,当focused为true时为LottieView设置启动动画
相反则停止动画

this.animation.play()
和
this.animation.reset()

还有需要注意的就是当进入主页的时候第一个icon默认选中,也就是动画的最后一帧
只需要判断位置设置progress字段即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要自定义react-navigationTabBar,你需要先创建一个自定义组件来代替默认的TabBar。你可以使用react-native的View和Text组件来创建一个自定义TabBar,然后使用react-navigation的TabNavigatorConfig来将自定义TabBar应用到你的TabNavigator上。 以下是一个简单的示例代码: ```javascript import React from 'react'; import { View, Text, TouchableOpacity } from 'react-native'; import { createBottomTabNavigator } from 'react-navigation'; const CustomTabBar = ({ navigation }) => { const { routes, index } = navigation.state; return ( <View style={{ flexDirection: 'row', height: 56 }}> {routes.map((route, idx) => { const isFocused = index === idx; const tintColor = isFocused ? 'blue' : 'gray'; return ( <TouchableOpacity key={route.key} onPress={() => navigation.navigate(route.routeName)} style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> <Text style={{ color: tintColor }}>{route.routeName}</Text> </TouchableOpacity> ); })} </View> ); }; const TabNavigator = createBottomTabNavigator( { Home: HomeScreen, Profile: ProfileScreen, }, { tabBarComponent: CustomTabBar, } ); ``` 在这个示例中,我们创建了一个名为CustomTabBar的自定义组件,并将其作为tabBarComponent选项传递给TabNavigator。CustomTabBar接收一个navigation对象作为参数,然后使用它来渲染包含所有选项卡的视图。我们使用TouchableOpacity组件为每个选项卡创建可点击的区域,并在被选中时改变选项卡标题的颜色。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值