我正在使用反应本机复活轮播。在轮播中,我正在渲染图像,当我点击图像时,它不会滚动到该图像。这是代码:
<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)代码> 不起作用。你能告诉我我错过了什么吗?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
如果您仔细查看文档,您必须在
scollTo中传递一个带有索引的对象:https://github.com/dohooo/react-native-reanimated-carousel/blob/main/docs/props。 md#ref像这样:
scrollTo({index: 1})