【发布时间】:2019-11-25 20:48:02
【问题描述】:
我尝试为视图设置动画以隐藏和显示它。持续时间可以打开它(500),但是当我关闭它时它不起作用,持续时间不受尊重(它直接关闭)。
这是我的代码:
const {height, width} = Dimensions.get('window');
const [initialHeight] = useState(new Animated.Value(0));
useEffect(() => {
if (state === true) {
Animated.timing(initialHeight, {
toValue: height - 400,
duration: 500,
}).start();
} else {
Animated.timing(initialHeight, {
toValue: 0,
duration: 500,
}).start();
}
}, [height, initialHeight, state]);
...
<Animated.View style={{height: initialHeight, paddingVertical: 12}}>
我错过了什么?
---编辑
我做了这个更改,但并没有解决问题:
const [initialHeight, setInitialHeight] = useState(new Animated.Value(0));
useEffect(() => {
if (state === true) {
Animated.timing(initialHeight, {
toValue: height - 400,
duration: 500,
}).start(() => {
setInitialHeight(new Animated.Value(height - 400));
});
} else {
Animated.timing(initialHeight, {
toValue: 0,
duration: 500,
}).start(() => {
setInitialHeight(new Animated.Value(0));
});
}
}, [height, initialHeight, state]);
【问题讨论】:
-
state变量是什么? -
这是一个布尔值。它是从包含组件传递的道具。为真时,动画起作用,为假时,就像视图没有动画一样,直接隐藏。
标签: reactjs react-native react-hooks react-animated