React Native ScrollView scrollTo弹簧动画

时间:2019-04-08 19:30:10

标签: react-native react-animated react-native-scrollview

我正在使用React Native的ScrollView的{​​{1}}方法来动画化滚动视图。我该如何像scrollTo中那样使该动画弹簧加载(不使用第三方库)?

1 个答案:

答案 0 :(得分:0)

跟踪滚动位置

<ScrollView
    ref={(ref) => { this.scrollView = ref; }}
    onScroll={(event) => {
        this.scrollY = event.nativeEvent.contentOffset.y;
    }}
    scrollEventThrottle={16}
>

然后

scrollTo(y) {
    if (!this.scrollY) this.scrollY = 0;
    const animatedValue = new Animated.Value(this.scrollY);
    const id = animatedValue.addListener(({ value }) => {
        this.scrollView.scrollTo({ x: 0, y: value, animated: false });
    });
   Animated.spring(animatedValue, { toValue: y }).start(() => { animatedValue.removeListener(id); /* finished callback */ });
}