提问人:marcusdel2112 提问时间:9/18/2023 最后编辑:marcusdel2112 更新时间:9/20/2023 访问量:286
React Native Reanimated 3 - 如何防止 View 在父级卸载时退出动画
React Native Reanimated 3 - How to prevent View exiting animation on parent unmount
问:
我正在使用 a 用于具有一些信息文本的卡片组件,我正在尝试使用 reanimated 使两个信息文本的简介在彼此之间淡入淡出。这工作正常,文本可以完美地淡入和淡出,但是当需要卸载此卡片组件以显示单独的组件时,整个卡片组件会等待子文本上的退出动画,然后消失。有没有办法在卸载父组件或以编程方式防止此退出动画发生?下面是代码的设置方式。<View/>
const [showSection1, setShowSection1] = useState(false)
return (
<>
{ showSection1 &&
<cardWithText/>
:
<someOtherComponent/>
}
<Button onPress={() => setShowSection1(false)} />
</>
)
}
const cardWithText = () => {
const [shownText, setShownText] = useState('text1') // the text to be shown in the card
const currentTimeout = useRef(0)
// when the component mounts being the timeout loop to toggle the shown text between the 'text1' and 'text2'
useEffect(() => {
if(!currentTimeout.current){
toggleText();
}
},[])
const toggleText = () => {
setShownText(prev => prev == "text1" ? "text2" : "text1")
currentTimeout.current = setTimeout(() => toggleShownText(), 2500)
}
return (
<View>
<Animated.Text entering={FadeIn.duration(600).delay(1500)} exiting={FadeOut.duration(600).delay(1200)} key={"card-info-" + infoText}>
{infoText}
</Animated.Text>
</View>
)
}
因此,当按下按钮显示第 2 部分并卸载卡时,整个卡组件 View 会等待文本组件退出动画,然后再卸载。我尝试了一些解决方法,例如将触发父项卸载的值向下传递到子卡,以防止在所述条件为真时退出动画,但这不起作用。我也无法找到任何针对这种情况的复活文档。
我也在这里的 GitHub 上找到了一个类似的线程 https://github.com/software-mansion/react-native-reanimated/discussions/2513 但找不到任何针对退出动画的解决方法,就像输入动画一样。
答:
0赞
marcusdel2112
9/20/2023
#1
我想出了一个体面的解决方法。另请注意,维护者目前正在开发一项功能,该功能将允许根据他的更新绕过退出/进入动画。
无论如何,可以通过将父视图设置为 来隐藏退出的动画。这样一来,孩子的退出动画将不会显示,并且会立即消失。display: 'none'
评论