从ScrollView的ScrollY位置有条件触发Animated.timing
我为整个应用程序的屏幕创建了一个模板。该模板利用动画标题组件来响应 ScrollView 内容的滚动位置。
我可以使用这个scrollY变量来插入项目,但我不知道如何使用该变量来触发Animated.timing
事件。
简单来说:
if (ScrollY > 30)
do Animated.timing oneif (ScrollY < 30)
do Animated.timing Two
现在我已经创建了一个不充分的使用 Animated.event() 侦听器
的解决方法:
<ScrollView
onScroll={Animated.event(
[{ nativeEvent: { contentOffset: { y: scrollListener } } }],
{
listener: (event) => {
handleScroll(event);
},
}
)}
...
handleScroll(event)
控制 Animated.timing
事件数组,每个事件都会操作有条件地从 setScrollPosition()
useState
变量:
const [scrollPosition, setScrollPosition] = useState(0);
const fixedHeaderTranslatePosition = useRef(new Animated.Value(4)).current;
const handleScroll = (event) => {
setScrollPosition(
event.nativeEvent.contentOffset.y +
(Platform.OS === 'ios' ? HEADER_MAX_HEIGHT : 0)
);
if (scrollPosition > 30 && headerType === 'Animated') {
Animated.timing(fixedHeaderTranslatePosition, {
toValue: 0,
useNativeDriver: true,
}).start();
...
}
虽然我的努力确实创建了一个功能组件,但存在一些小问题。 首先,event.nativeEvent.contentOffset.y
似乎会导致 Android 设备出现延迟。这在 fixedHeaderTranslatePosition
中最为明显。
此外,从逻辑上讲,利用 Animated.event
中已声明的 scrollListener
会更有意义。所以我的问题是:如何在 handleScroll()
中使用 ScrollY
变量 (scrollListener
)?
I've created a template for screens throughout my app. The template utilises Animated Header components that respond to the scrollY position of the ScrollView content.
I'm able to use this scrollY variable to interpolate items, but I can't figure out how to use the variable to trigger Animated.timing
events.
In simple terms:
if (ScrollY > 30)
do Animated.timing oneif (ScrollY < 30)
do Animated.timing two
For now I've created an inadequate workaround using the Animated.event() listener
:
<ScrollView
onScroll={Animated.event(
[{ nativeEvent: { contentOffset: { y: scrollListener } } }],
{
listener: (event) => {
handleScroll(event);
},
}
)}
...
The handleScroll(event)
controls an array of Animated.timing
events, each of which operates conditionally from a setScrollPosition()
useState
variable:
const [scrollPosition, setScrollPosition] = useState(0);
const fixedHeaderTranslatePosition = useRef(new Animated.Value(4)).current;
const handleScroll = (event) => {
setScrollPosition(
event.nativeEvent.contentOffset.y +
(Platform.OS === 'ios' ? HEADER_MAX_HEIGHT : 0)
);
if (scrollPosition > 30 && headerType === 'Animated') {
Animated.timing(fixedHeaderTranslatePosition, {
toValue: 0,
useNativeDriver: true,
}).start();
...
}
Whilst my efforts do create a functional component, there are some minor problems.
Firstly, the event.nativeEvent.contentOffset.y
seems to cause a lag on Android devices. This is most noticeable with the fixedHeaderTranslatePosition
.
Also logically it would make much more sense to utilise the already declared scrollListener
from the Animated.event
. So my question is: How can I utilise the ScrollY
variable (scrollListener
) inside my handleScroll()
? ????
The ScrollY
variable is not noted above since it makes more sense in context. Here's a working snack: https://snack.expo.dev/@dazzerr/animated-header-example . You can search for "TODO" to find the areas that I believe require attention.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论