Arn*_*nik 10 reactjs react-native
我一直在使用Scrollview
它包含一个包含页面指示符作为数字的列表视图.当我点击它们时,我希望它们滚动,以便所选的数字位于中心
喜欢
4 5 6 [7] 8 9
这里选择7是数字,当我在这里点击9时
scrollview向左滚动,以便视图显示为
7 8 [9] 10 11
我正在使用scrollTo(x : calculated , y : 0 , animated : true)
滚动太慢了.我希望滚动快速或可由参数控制,我已尝试持续时间,scrollTo
但这不起作用.
目前这是不可能的.调用scrollTo会调用scrollResponderScrollTo,然后调用ios实现或android实现.这将是必须实现滚动速度的点.
这是一个非常简洁的解决方案,它使用滚动视图的内容高度来滚动整个视图(在安装时)。但是,可以使用相同的技巧(向动画值添加侦听器)来创建一个滚动函数,该函数可以在任何给定时刻(针对任何给定值)由某些事件触发。
import { useEffect, useRef, useState } from 'react'
import { Animated, Easing, ScrollView } from 'react-native'
const SlowAutoScroller = ({ children }) => {
const scrollRef = useRef()
const scrollAnimation = useRef(new Animated.Value(0))
const [contentHeight, setContentHeight] = useState(0)
useEffect(() => {
scrollAnimation.current.addListener((animation) => {
scrollRef.current &&
scrollRef.current.scrollTo({
y: animation.value,
animated: false,
})
})
if (contentHeight) {
Animated.timing(scrollAnimation.current, {
toValue: contentHeight,
duration: contentHeight * 100,
useNativeDriver: true,
easing: Easing.linear,
}).start()
}
return () => scrollAnimation.current.removeAllListeners()
}, [contentHeight])
return (
<Animated.ScrollView
ref={scrollRef}
onContentSizeChange={(width, height) => {
setContentHeight(height)
}}
onScrollBeginDrag={() => scrollAnimation.current.stopAnimation()}
>
{children}
</Animated.ScrollView>
)
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4277 次 |
最近记录: |