javascript - 通过按下动画屏幕上的按钮滚动 - React Native
问题描述
我是 React Native 的新手。在我的仪表板页面上,3 个屏幕组件出现了滚动效果。我只能在屏幕上滚动页面时执行此操作。当单击此处代码底部的箭头 svg 时,我希望它切换到另一个屏幕。我怎样才能做到这一点?
我的代码:
import React, { useRef } from "react"
import { Animated, View } from "react-native"
import { LEVELSELECTION } from "../../navigation/routesNames"
import { units } from "../../theme/Units"
import ArrowSvg from '../../assets/svgs/arrow.svg'
import DashboardScreen from "../components/DashboardScreen"
import styles from "./styles/DashboardStyles"
export default function Dashboard({ navigation }) {
function nextScreen() {
navigation.navigate(LEVELSELECTION)
}
const slides = [
{
id: "1",
page: DashboardScreen(1)
},
{
id: "2",
page: DashboardScreen(2)
},
{
id: "3",
page: DashboardScreen(3, nextScreen)
},
]
const Indicator = ({ scrollX }) => {
return (
<View style={styles.indicatorContainer}>
{slides.map((_, index) => {
const inputRange = [(index - 1) * units.width, index * units.width, (index + 1) * units.width]
const scale = scrollX.interpolate({
inputRange,
outputRange: [0.7, 1.4, 0.7],
extrapolate: "clamp",
})
const opacity = scrollX.interpolate({
inputRange,
outputRange: [0.5, 1, 0.5],
extrapolate: "clamp",
})
return (
<Animated.View
key={`indicator-${index}`}
style={[
styles.indicator,
{
transform: [{ scale }],
opacity
}
]}
/>
)
})}
</View>
)
}
const scrollX = useRef(new Animated.Value(0)).current
return (
<View style={styles.container}>
<Animated.FlatList
data={slides}
keyExtractor={item => item.id}
horizontal
pagingEnabled
scrollEventThrottle={32}
onScroll={Animated.event(
[{ nativeEvent: { contentOffset: { x: scrollX } } }],
{
useNativeDriver: false,
}
)}
showsHorizontalScrollIndicator={false}
renderItem={({ item }) => {
return (
<View>
{item.page}
</View>
)
}} />
<View style={styles.indicatorLocation}>
<Indicator scrollX={scrollX}></Indicator>
</View>
<View style={styles.arrow}>
<ArrowSvg />
</View>
</View>
)
}
在我的代码底部,您可以看到 ArrowSvg。我想让你给这个箭头添加滚动效果。
解决方案
推荐阅读
- mysql - SQL获取Select中列的总和
- r - 使用 C 库中的 TclTK 返回整数
- python - PySimpleGUI 用于带有可选参数的脚本?
- c++ - 具有增加的所有者线程优先级的同步原语
- python-3.x - 如何使用 GPflow 进行预测 - 使用简单的数据输入运行 GPC?无法在不同数据上运行示例笔记本中的代码
- javascript - 如何在 Express 中将请求正文的数据从一条路由移动到另一条路由
- python - 在不同的日期时间索引上组合 DataFrame
- javascript - 使用曲线将输入范围弧形拇指连接到滑块
- node.js - 使用 node-postgres 以编程方式创建数据库
- enums - Rails 6:类中的枚举