kon*_*ala 2 animation reactjs react-native react-navigation
我正在尝试为菜单徽标设置动画以在单击时旋转。当它向上旋转时,我成功地获得了旋转,但它在向下旋转时直接变为 0,而不是通过旋转动画。
这是我的组件:
import React from 'react';
import { TouchableOpacity, Animated } from 'react-native';
import PropTypes from 'prop-types';
import styles from './styles';
const TabIcon = ({
route,
renderIcon,
onPress,
focused,
menuToggled,
activeTintColor,
inactiveTintColor,
}) => {
const isMenuLogo = route.params && route.params.navigationDisabled;
const animation = new Animated.Value(0);
Animated.timing(animation, {
toValue: menuToggled ? 1 : 0,
duration: 200,
useNativeDriver: true,
}).start();
const rotateInterpolate = animation.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '180deg'],
});
const animatedStyles = { transform: [{ rotate: rotateInterpolate }] };
const logoStyles = [animatedStyles, styles.logoStyle];
return (
<TouchableOpacity
style={styles.tabStyle}
onPress={onPress}
activeOpacity={isMenuLogo && 1}
>
<Animated.View style={isMenuLogo ? logoStyles : null}>
{
renderIcon({
route,
focused,
tintColor: focused
? activeTintColor
: inactiveTintColor,
})
}
</Animated.View>
</TouchableOpacity>
);
};
TabIcon.propTypes = {
route: PropTypes.shape({
key: PropTypes.string,
}).isRequired,
renderIcon: PropTypes.func.isRequired,
onPress: PropTypes.func,
focused: PropTypes.bool,
menuToggled: PropTypes.bool,
activeTintColor: PropTypes.string.isRequired,
inactiveTintColor: PropTypes.string.isRequired,
};
TabIcon.defaultProps = {
onPress: () => {},
focused: false,
menuToggled: false,
};
export default TabIcon;
Run Code Online (Sandbox Code Playgroud)
在实际旋转它之前,我首先检查它是否已被切换。正在另一个显示自定义底部选项卡导航的父组件中调用此组件。
当它向下旋转时,我应该为它做一个不同的动画,还是我当前动画中缺少配置?
任何帮助和建议将不胜感激。谢谢你。
我认为这个问题与这样一个事实有关,即当您设置初始值时,animation
因为它始终设置为0
它不会反映切换菜单时的更改。
你需要改变:
const animation = new Animated.Value(0);
Run Code Online (Sandbox Code Playgroud)
到
const animation = new Animated.Value(menuToggled ? 0 : 1);
Run Code Online (Sandbox Code Playgroud)
尽管进行这种更改会导致不同的问题。因为menuToggled
影响动画的开始和结束位置,图标现在将从结束位置旋转到正确的开始位置。这并不理想。
但是,我们可以通过为 设置默认值 null 来解决这个问题menuToggled
。然后将动画包装在if-statement
仅在menuToggled
不是 时运行的null
。
这是基于您的初始代码的示例:
import React from 'react';
import { View, StyleSheet, Animated, TouchableOpacity } from 'react-native';
import { Ionicons } from '@expo/vector-icons';
const TabIcon = ({
onPress,
menuToggled
}) => {
const logoStyles = [styles.logoStyle];
if (menuToggled !== null) {
const animation = new Animated.Value(menuToggled ? 0 : 1);
Animated.timing(animation, {
toValue: menuToggled ? 1 : 0,
duration: 200,
useNativeDriver: true
}).start();
const rotateInterpolate = animation.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '180deg']
});
const animatedStyles = { transform: [{ rotate: rotateInterpolate }] };
logoStyles.push(animatedStyles);
}
return (
<TouchableOpacity
style={styles.tabStyle}
onPress={onPress}
>
<Animated.View style={logoStyles}>
<Ionicons name="md-checkmark-circle" size={32} color="green" />
</Animated.View>
</TouchableOpacity>
);
};
export default class App extends React.Component {
state = {
menuToggled: null
}
toggleMenu = () => {
this.setState(prevState => {
return { menuToggled: !prevState.menuToggled };
});
}
render () {
return (
<View style={styles.container}>
<TabIcon
onPress={this.toggleMenu}
menuToggled={this.state.menuToggled}
/>
</View>
);
}
}
Run Code Online (Sandbox Code Playgroud)
我剥离了你的TabIcon
组件,因为那里有很多与动画无关的东西。您应该能够轻松地将我所做的合并到您自己的组件中。https://snack.expo.io/@andypandy/rotating-icon
归档时间: |
|
查看次数: |
5401 次 |
最近记录: |