javascript - Animated.View 仅适用于展开,不适用于折叠
问题描述
我在使用<Animated.View />
. 以下仅适用于toValue
设置为40
。它很好地扩展并且动画有效。在折叠时(将 设置toValue
为0
),它只是混合在一起 - 好像可见性设置为none
。这是为什么?
const Foo = () => {
const [bounceValue, setBounceValue] = useState(new Animated.Value(0));
const [collapsed, setCollapsed] = useState(true);
const height = { height: bounceValue };
const _slideInOut = () => {
let toValue;
if (collapsed) {
toValue = 40;
setCollapsed(false);
} else {
toValue = 0;
setCollapsed(true);
}
Animated.timing(bounceValue, {
toValue: toValue,
duration: 400,
useNativeDriver: false
}).start();
}
return <TouchableOpacity
activeOpacity={1}
style={[
styles.container,
...
]}
onPress={() => _slideInOut()}>
<Animated.View
style={[
height,
styles.menu,
]}>
...
</Animated.View>
</TouchableOpacity>
}
const styles = StyleSheet.create({
container: {
width: '100%',
top: 0,
height: 10,
position: 'absolute',
},
menu: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
backgroundColor: 'white',
position: 'absolute',
top: 10,
width: '100%'
}
})
解决方案
您想为此使用 ref:
const bounceValue = useRef(new Animated.Value(0)).current;
推荐阅读
- java - 如何使用指定参数启动 jar 文件
- javascript - 出现错误未捕获错误:[vuex] getters 应该是函数,但“getters.getters”是 {}。将存储拆分为模块时在 VUEX 存储中
- regex - 通过排除双引号内的逗号来拆分字符串
- android - 如何在颤动中从 BLE 设备(RFID 手持阅读器设备)访问数据
- jwt - Thingsboard:如何检索 JWT 令牌并定义到期时间?
- python - 最长子串
- firebase - 我是 Firebase 的新手,对现在发生的事情感到非常困惑。作为参考,我正在使用带有颤振的 Firebase。不断报错
- ansible - 从ansible列表中删除空元素
- axios - 使用axios下载pdf失败(空白页)
- java - 如何使用Spring以只读和读写方式进行数据库路由