从ScrollView的ScrollY位置有条件触发Animated.timing

发布于 2025-01-19 05:51:09 字数 1552 浏览 4 评论 0原文

我为整个应用程序的屏幕创建了一个模板。该模板利用动画标题组件来响应 ScrollView 内容的滚动位置。

我可以使用这个scrollY变量来插入项目,但我不知道如何使用该变量来触发Animated.timing事件。

简单来说:

  • if (ScrollY > 30) do Animated.timing one
  • if (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 one
  • if (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文