如何为react-native-carousel制作自定义点

zbdgwd5y  于 2022-11-17  发布在  React
关注(0)|答案(1)|浏览(108)

我必须为carousel创建分页点,如下所示:活动点应为渐变色,非活动点应为灰色here is active dot's design
当用户开始滑动时,当前活动点从白色转换为灰色。我在处理这部分时遇到了问题。design for dots when sliding starts
到目前为止,我所尝试的是:

  1. const renderCarouselDots = () => {
  2. return (
  3. <View style={styles.dotsView}>
  4. <FlatList
  5. style={{alignSelf: 'center'}}
  6. horizontal
  7. data={carouselData}
  8. renderItem={({item, index}) =>
  9. activeSlide === index ? (
  10. isSliderSwiping ? (
  11. <View style={[styles.carouselDot, {backgroundColor: "white"}]}></View>
  12. ) : (
  13. <GradientBackground style={styles.carouselDot} />
  14. )
  15. ) : (
  16. <View
  17. style={[styles.carouselDot, styles.inactiveCarouselDot]}></View>
  18. )
  19. }
  20. />
  21. </View>
  22. );};

carousel的代码是:

  1. <Carousel
  2. // onScroll={() => console.log("scroll start")}
  3. renderItem={renderCarouselItem}
  4. data={carouselData}
  5. sliderWidth={getScreenWidthHeight().width}
  6. itemWidth={getScreenWidthHeight().width}
  7. onBeforeSnapToItem={(i) => setIsSwiping(true)}
  8. onSnapToItem={(index) => {
  9. setActiveSlide(index);
  10. setIsSwiping(false);
  11. console.log(index);
  12. }}
  13. // onScrollBeginDrag={(e) => console.log(e)}
  14. // autoplay
  15. />
  16. {renderCarouselDots()}

我目前所取得的成就... dots when there is no sliding etc.dots sometime after sliding starts
我知道如何在不同颜色之间切换动画。但我不确定如何知道用户何时开始滑动。我曾尝试使用onBeforeSnapToItem属性,以便在幻灯片上将点转换为白色视图,但这在滑动开始后触发。
我想知道如何实现这个动画之间的渐变到白色和灰色的点,以及何时调用这个动画。任何帮助将不胜感激!

5fjcxozz

5fjcxozz1#

尝试以下代码

  1. onItemSwipe = ({ nativeEvent }) => {
  2. // below variable will give active index of the item that is visible
  3. const _activeSlide = Math.floor(parseInt(nativeEvent.contentOffset.x) / parseInt(nativeEvent.layoutMeasurement.width));
  4. // as it is onMomentumScrollEnd this activeSlide will only change on complete scroll change
  5. this.setState({
  6. activeSlide_: _activeSlide,
  7. });
  8. };
  9. <FlatList {...otherProps} onMomentumScrollEnd={this.onItemSwipe} />

相关问题