gpt4 book ai didi

javascript - 在滚动 React Native Animated 上做动画但闪烁

转载 作者:行者123 更新时间:2023-11-30 14:06:00 32 4
gpt4 key购买 nike

在我的 React native 应用程序中,我试图为 View 的不透明度设置动画。当我滚动时,我看到动画完成了工作,但它同时闪烁。我不知道为什么。

视频示例:https://cdn.discordapp.com/attachments/102861040538120192/560165613092339734/video.mov

这是我写的代码

const Scrollable = () => {
const largeHeaderSize = useState({
height: 0,
y: 0
});

const animatedValueScrollY = new Animated.Value(largeHeaderSize.height);
const [scrollY, setScrollY] = useState(0);

const headerOpacity = animatedValueScrollY.interpolate({
inputRange: [0, largeHeaderSize.height],
outputRange: [1, 0],
extrapolate: "clamp"
});

return (
<SafeAreaView>
<Animated.View
style={{
borderBottomWidth:
scrollY >= largeHeaderSize.height ? StyleSheet.hairlineWidth : 0
}}>
<View>
<Animated.View style={{ zIndex: 1, opacity: headerOpacity }}>
<Text>Title</Text>
</Animated.View>
</View>
</Animated.View>
<Animated.ScrollView
onScroll={Animated.event(
[{ nativeEvent: { contentOffset: { y: animatedValueScrollY } } }],
{
useNativeDriver: true,
listener: event => setScrollY(event.nativeEvent.contentOffset.y)
}
)}
scrollEventThrottle={16}
contentInset={{
top: 0,
bottom: 50
}}
contentOffset={{
y: 0
}}
/>
</SafeAreaView>
);
};

我该如何解决?

最佳答案

解决方案是像这样使用 useRef 钩子(Hook):

const animatedValueScrollY = useRef(new Animated.Value(0))

const headerOpacity = animatedValueScrollY.current.interpolate({
inputRange: [0, largeHeaderSize.height],
outputRange: [1, 0],
extrapolate: 'clamp'
});

关于javascript - 在滚动 React Native Animated 上做动画但闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55373138/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com