我正在使用 React Native 动画旋转木马。在轮播中,我正在渲染图像,当我点击图像时,它不会滚动到该图像。这是代码:
<Carousel
ref={carouselRef}
loop
width={Dimensions.get("window").width}
height={300 / 2}
data={carTypes}
mode="parallax"
defaultIndex={1}
style={{ alignSelf: "center",justifyContent: "center", alignContent: "center"}}
scrollAnimationDuration={1000}
onSnapToItem={(index) => setActiveSlide(index)}
renderItem={({ item, index }) => (
<TouchableOpacity
**onPress={() => carouselRef.current.scrollTo(index)}**
activeOpacity={0.1}
style={{
backgroundColor: activeSlide !== index ? carouselActiveSlide(index) : "#3887EF",
borderWidth: 1,
justifyContent: 'center',
transform: [{ rotate: "32deg" }],
borderRadius: 32,
width: 50
}}
/>
我试图测试其他事件,如
.next()
, .prev()
并且它们按预期工作但 .scrollTo(index)
不起作用。你能告诉我我错过了什么吗?
如果你仔细查看文档,你必须在
scollTo
中传递一个对象,其中包含索引:https://github.com/dohooo/react-native-reanimated-carousel/blob/main/docs/道具.md#ref
像这样:
scrollTo({index: 1})