React native 为什么自定义底部选项卡栏导航器的所有图标同时移动?
我正在尝试在react native中创建一个CustomBottomTabNavigator。现在我已经应用了线性渐变,并在选项卡顶部添加了图标。我的目标是当焦点在图标上时,向上移动图标,但由于某些原因,当焦点仅在一个图标上时,所有图标都向上移动 代码如下:React native 为什么自定义底部选项卡栏导航器的所有图标同时移动?,react-native,animation,react-navigation,React Native,Animation,React Navigation,我正在尝试在react native中创建一个CustomBottomTabNavigator。现在我已经应用了线性渐变,并在选项卡顶部添加了图标。我的目标是当焦点在图标上时,向上移动图标,但由于某些原因,当焦点仅在一个图标上时,所有图标都向上移动 代码如下: import React, { useRef } from "react"; import { View, Text, StyleSheet, Animated, TouchableOpacity,
import React, { useRef } from "react";
import {
View,
Text,
StyleSheet,
Animated,
TouchableOpacity,
} from "react-native";
import * as Icons from "@expo/vector-icons";
import { LinearGradient } from "expo-linear-gradient";
const CustomTabBar = ({ state, descriptors, navigation }) => {
let icons_name = ["home", "search", "tv", "user"];
const animatedValueHome = useRef(new Animated.Value(0)).current;
const translateY = animatedValueHome.interpolate({
inputRange: [50, 100, 150],
outputRange: [25, 50, 75],
});
const animationHome = (focus, name) => {
console.log("name", name);
navigation.navigate(name);
if (focus === true) {
Animated.timing(animatedValueHome, {
toValue: -25,
duration: 1000,
useNativeDriver: false,
}).start();
} else {
Animated.timing(animatedValueHome, {
toValue: 0,
duration: 1000,
useNativeDriver: false,
}).start();
}
};
return (
<LinearGradient
colors={["#181823", "#3A3A46", "#3A3A46"]}
start={{ x: 0, y: 0.5 }}
end={{ x: 1, y: 0.5 }}
locations={[0.2, 0.6, 0.3]}
style={styles.container}
>
<View style={styles.tabs}>
{state.routes.map((route, index) => {
const isFocused = state.index === index;
return (
<Animated.View
key={index}
style={{
flex: 1,
flexDirection: "row",
transform: [{ translateY }],
}}
>
<Icons.Feather
name={icons_name[`${index}`]}
size={24}
color="#fff"
onPress={() => animationHome(isFocused, route.name)}
/>
</Animated.View>
);
})}
</View>
</LinearGradient>
);
};
export default CustomTabBar;
const styles = StyleSheet.create({
container: {
position: "absolute",
height: 40,
bottom: 20,
right: 30,
left: 20,
elevation: 2,
borderRadius: 20,
},
tabs: {
flex: 1,
flexDirection: "row",
alignItems: "center",
marginLeft: 48,
},
});
import React,{useRef}来自“React”;
进口{
看法
文本,
样式表,
有生气的
可触摸不透明度,
}从“反应本族语”;
从“@expo/vector Icons”导入*作为图标;
从“expo linear gradient”导入{LinearGradient};
const CustomTabBar=({状态,描述符,导航})=>{
让图标_name=[“主页”、“搜索”、“电视”、“用户”];
const animatedValueHome=useRef(新的Animated.Value(0)).current;
const translateY=animatedValueHome.interpolate({
输入范围:[50100150],
输出范围:[25,50,75],
});
常量animationHome=(焦点,名称)=>{
console.log(“name”,name);
导航。导航(名称);
如果(焦点===真){
动画。计时(动画值主页{
toValue:-25,
持续时间:1000,
useNativeDriver:错误,
}).start();
}否则{
动画。计时(动画值主页{
toValue:0,
持续时间:1000,
useNativeDriver:错误,
}).start();
}
};
返回(
{state.routes.map((路由,索引)=>{
const isFocused=state.index==index;
返回(
。我正在使用react native的动画API来实现此动画。让每个子组件都有自己的动画值
// In the parent component
{state.routes.map((route, index) => {
const isFocused = state.index === index;
return <Child isFocused={isFocused} />;
})}
// Then for each child
const Child = ({ isFocused }) => {
const animatedValueHome = useRef(new Animated.Value(0)).current;
const translateY = animatedValueHome.interpolate({
inputRange: [50, 100, 150],
outputRange: [25, 50, 75],
});
const animationHome = (focus, name) => {
console.log("name", name);
navigation.navigate(name);
if (focus === true) {
Animated.timing(animatedValueHome, {
toValue: -25,
duration: 1000,
useNativeDriver: false,
}).start();
} else {
Animated.timing(animatedValueHome, {
toValue: 0,
duration: 1000,
useNativeDriver: false,
}).start();
}
};
return (
<Animated.View
style={{
transform: [{ translateY }],
}}
>
<Icons.Feather onPress={() => animationHome(isFocused, route.name)}/>
</Animated.View>
);
}
//在父组件中
{state.routes.map((路由,索引)=>{
const isFocused=state.index==index;
返回;
})}
//然后给每个孩子
const Child=({isFocused})=>{
const animatedValueHome=useRef(新的Animated.Value(0)).current;
const translateY=animatedValueHome.interpolate({
输入范围:[50100150],
输出范围:[25,50,75],
});
常量animationHome=(焦点,名称)=>{
console.log(“name”,name);
导航。导航(名称);
如果(焦点===真){
动画。计时(动画值主页{
toValue:-25,
持续时间:1000,
useNativeDriver:错误,
}).start();
}否则{
动画。计时(动画值主页{
toValue:0,
持续时间:1000,
useNativeDriver:错误,
}).start();
}
};
返回(
animationHome(isFocused,route.name)}/>
);
}
它们都使用transform:[{translateY}]
。相同的translateY。感谢您的帮助,但我很想知道当焦点在它们上时动画将如何自动上升,因为现在图标只有在我单击它们时才会上升。然后添加一个效果,当焦点道具更新时调用动画开始。useffect()=>动画主页(聚焦),[isFocused];