Javascript 在“滚动”上,执行动画,但会闪烁
在我的react原生应用程序中,我尝试设置视图不透明度的动画。 当我滚动时,我看到动画完成了工作,但它同时闪烁。我不知道为什么 视频示例: 这是我写的代码Javascript 在“滚动”上,执行动画,但会闪烁,javascript,ios,react-native,animation,react-hooks,Javascript,Ios,React Native,Animation,React Hooks,在我的react原生应用程序中,我尝试设置视图不透明度的动画。 当我滚动时,我看到动画完成了工作,但它同时闪烁。我不知道为什么 视频示例: 这是我写的代码 const Scrollable = () => { const largeHeaderSize = useState({ height: 0, y: 0 }); const animatedValueScrollY = new Animated.Value(largeHeaderSize.height);
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>
);
};
const Scrollable=()=>{
const largeHeaderSize=useState({
高度:0,,
y:0
});
const animatedValueScrollY=新的Animated.Value(largeHeaderSize.height);
const[scrollY,setScrollY]=useState(0);
常数头容量=动画值Scrolly.interpolate({
输入范围:[0,大标题.高度],
outputRange:[1,0],
外推:“夹具”
});
返回(
=largeHeaderSize.height?样式表.hairlineWidth:0
}}>
标题
setScroly(event.nativeEvent.contentOffset.y)
}
)}
scrollEventThrottle={16}
内容插图={{
排名:0,
底数:50
}}
内容偏移={{
y:0
}}
/>
);
};
我怎样才能解决这个问题 解决方案是像这样使用useRef钩子:
const animatedValueScrollY = useRef(new Animated.Value(0))
const headerOpacity = animatedValueScrollY.current.interpolate({
inputRange: [0, largeHeaderSize.height],
outputRange: [1, 0],
extrapolate: 'clamp'
});
尝试在测试动画时禁用JS开发模式。。。发展商menu@HendEl-Sahli我在真实设备上也有问题吗?你认为它能解决这个问题吗?模拟器和物理设备都是一样的。。。测试动画时,建议始终禁用JS Dev模式。这并不能解决问题:(