[英]Animated.View only works on expanding, not on collapsing
I am having trouble using <Animated.View />
.我在使用
<Animated.View />
时遇到问题。 The following only works on when the toValue
is set to 40
.以下仅适用于
toValue
设置为40
时。 It expands nicely and the animation works.它可以很好地扩展,并且 animation 可以正常工作。 On collapsing (setting the
toValue
to 0
), it just blends out - as if the visibility is set to none
.在折叠时(将
toValue
设置为0
),它只是混合出来 - 就好像可见性设置为none
一样。 Why is that?这是为什么?
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%'
}
})
you want to use a ref for this:您想为此使用 ref:
const bounceValue = useRef(new Animated.Value(0)).current;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.