react-native - 反应导航自定义标签栏动画不起作用
问题描述
我尝试为标签栏自定义添加动画,但它似乎不起作用
我使用反应导航和自定义标签栏(https://reactnavigation.org/docs/bottom-tab-navigator/#props)
我的代码:
Tabbar.js
import Animated from 'react-native-reanimated';
import { styles } from './style';
import Tab from './Tab';
function TabBar({ state, descriptors, navigation }) {
const focusedOptions = descriptors[state.routes[state.index].key].options;
const safeAreainsets = useSafeAreaInsets();
if (focusedOptions.tabBarVisible === false) {
return null;
}
const activeAni = new Animated.Value(0);
return (
<View style={styles.tabbar}>
<View style={styles.container}>
<View style={styles.routeContent}>
{state.routes.map((route, index) => {
const { options } = descriptors[route.key];
const isFocused = state.index === index;
const onPress = () => {
activeAni.setValue(index);
const event = navigation.emit({
type: 'tabPress',
target: route.key,
canPreventDefault: false,
});
if (!isFocused && !event.defaultPrevented) {
navigation.navigate(route.name);
}
};
const onLongPress = () => {
navigation.emit({
type: 'tabLongPress',
target: route.key,
});
};
return (
<Tab
key={`tabbar_${index}`}
onPress={onPress}
onLongPress={onLongPress}
isFocused={isFocused}
index={index}
options={options}
activeAnimated={activeAni}
/>
);
})}
</View>
<View style={{ paddingBottom: safeAreainsets.bottom }} />
</View>
</View>
);
}
Tab.js
import Animated, { eq, multiply } from 'react-native-reanimated';
import { withTransition } from 'react-native-redash';
import { styles } from './style';
const ICON_SIZE = 27;
function Tab({
isFocused,
options,
index,
activeAnimated,
onPress,
onLongPress,
}) {
const isActive = eq(activeAnimated, index);
const activeTransition = withTransition(isActive);
const width = multiply(activeTransition, ICON_SIZE);
return (
<TouchableWithoutFeedback
accessibilityRole="button"
accessibilityState={isFocused ? { selected: true } : {}}
accessibilityLabel={options.tabBarAccessibilityLabel}
testID={options.tabBarTestID}
onPress={onPress}
onLongPress={onLongPress}
style={styles.button}>
<View style={styles.stack}>
<View style={styles.iconBox}>
<Icon name={options.tabBarIconName} style={styles.defaultIcon} />
</View>
<Animated.View style={{ overflow: 'hidden', width }}>
{cloneElement(
<Icon
name={options.tabbarIconActive}
style={[styles.defaultIcon, styles.focusIcon]}
/>,
)}
</Animated.View>
</View>
</TouchableWithoutFeedback>
);
}
当我像下面这样评论 onPress 事件时,动画效果很好
const onPress = () => {
activeAni.setValue(index);
// const event = navigation.emit({
// type: 'tabPress',
// target: route.key,
// canPreventDefault: false,
// });
// if (!isFocused && !event.defaultPrevented) {
// navigation.navigate(route.name);
// }
};
似乎单击时,选项卡栏会重新呈现,然后动画将不起作用,我的情况有什么解决方案吗?谢谢。
解决方案
推荐阅读
- r - 如何交替 3(数字)列的数值并将其放入一列?
- javascript - 是否可以使用 JavaScript 手动触发 DOM 事件?
- c++ - 错误:- 使用二次方程拆分中间项的程序
- chef-infra - 获取 STDERR:用法:升级到 Chef 客户端 16 后运行“mount”命令时
- python - 将随机矩阵变为半正定
- istio - istio 中的 VirtualService 和 Gateway 有什么区别?
- java - 如何从同一类中的主方法访问非静态方法?
- weblogic - weblogic 从 11g 更新到 12c
- angular - 如何以角度验证子组件上的模板驱动表单
- c# - C# 单字符日期格式