简体   繁体   English

Animated.View 仅适用于展开,不适用于折叠

[英]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.

相关问题 为什么 Animated.View 中的 ScrollView 不起作用? - why ScrollView inside Animated.View not working? PanResponder在第二次拖动时将Animated.View捕捉回原始位置 - PanResponder snaps Animated.View back to original position on second drag 如何使 React Native Animated.View 可点击? - How to make React Native Animated.View clickable? 为 React-Native 应用开玩笑测试 Animated.View - Jest test Animated.View for React-Native app 当Animated.View将Animated.ScrollView作为子项时,PanResponder在Android上无法正常工作 - PanResponder not working properly on Android when Animated.View has an Animated.ScrollView as a child React Native - Animated.View 与常规 View 组件之间的性能差异 - React Native - Performance difference between Animated.View vs regular View component 当要设置动画的项目位于可滚动视图前面时,Panresponder 或 Animated.View 不起作用 - Panresponder or Animated.View doesnt work when the item to animate is in front of a scrollable view 在 Animated.View 中反应 Native ScrollView 以制作类似 Bumble 的滑动系统 - React Native ScrollView inside an Animated.View to make Bumble-like swipe system 在GridView中展开/折叠项目 - Expanding/Collapsing items in GridView 在网页上扩展/折叠Div - Expanding/Collapsing Divs on a Webpage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM