我正在使用反應(yīng)本機(jī)復(fù)活輪播。在輪播中,我正在渲染圖像,當(dāng)我點(diǎn)擊圖像時(shí),它不會(huì)滾動(dòng)到該圖像。這是代碼:
<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()
,它們是否按預(yù)期工作,但 .scrollTo(index)代碼> 不起作用。你能告訴我我錯(cuò)過了什么嗎?
如果您仔細(xì)查看文檔,您必須在 scollTo
中傳遞一個(gè)帶有索引的對象:https://github.com/dohooo/react-native-reanimated-carousel/blob/main/docs/props。 md#ref
像這樣:
scrollTo({index: 1})
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://www.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號