react-native - React-Native 自动向下滚动
问题描述
我有一个带有一个 ScrollView 和一个按钮的应用程序,里面有一个长文本:
return (
<ScrollView>
<Button onPress={slowlyScrollDown} title="Slowly scroll a bit down..." />
<Text>
[ Very long text here where user has to scroll ]
</Text>
</ScrollView>
)
当用户按下按钮时,我想慢慢向下滚动一点,这样他就可以看到文本的前 5-10 行。
我会接受任何提供一段代码的答案,我可以如何实现它。
解决方案
我不确定它会起作用,但这是方向:
const [offset,setOffset] = useState(0);
const scrollViewRef = useRef();
const slowlyScrollDown = () => {
const y = offset + 80;
scrollViewRef.current.scrollTo({x: 0, y, animated: true});
setOffset(y);
}
return (
<ScrollView ref={scrollViewRef} >
<Button onPress={slowlyScrollDown} title="Slowly scroll a bit down..." />
<Text>
[ Very long text here where user has to scroll ]
</Text>
</ScrollView>
)
推荐阅读
- c++ - 如何计算选择排序中的比较?
- shell - 如何匹配分布在两条线上的一组字符串?
- python - 是否可以每 n 次迭代重新启动 spyder 控制台并再次运行脚本?
- mediawiki - 如何在维基百科文章中显示维基数据附加链接
- xcode - Xcode 10.2 Beta 断点不起作用 - LLDB RPC 服务器已崩溃
- node.js - (ASK) 我们如何使用 AWS api 网关调试本地 websocket
- image - 如何使用其 colorFilter 保存来自 DecorationImage 的图像 - Flutter
- node.js - Sequelize where 嵌套条件包括进入内部连接
- jquery - jQuery-UI .dialog("moveToTop") 即使成功运行也会引发错误
- printing - 打印时关闭电源后 Zebra 需要重新校准