关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回

React Native 0.52实现轮播图效果

发布时间:2022-09-27 06:00:13

React Native 0.52实现轮播图效果?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

1、通过npm安装react-native-swiper

npm install react-native-swiper --save

2、在recommend.js引入react-native-swiper

import Swiper from 'react-native-swiper';

3、用 react-native-swiper 可以很容易的实现轮播的效果

  • showButtons —— 是否显示左右翻页按钮

  • autoPlay —— 是否自动播放

  • paginationStyle —— 包含小点点的容器的样式,这里用来调整位置

  • dotStyle —— 小点点的样式

  • activeDotStyle —— 当前被激活的小点点的样式

<Swiper
   style={styles.wrapper}
   height={width*40/75}
   showsButtons={false}
   autoplay={true}
   paginationStyle={styles.paginationStyle}
   dotStyle={styles.dotStyle}
   activeDotStyle={styles.activeDotStyle}
>
    <Image source={require('../../img/1.jpg')} style={styles.bannerImg} />
    <Image source={require('../../img/2.jpg')} style={styles.bannerImg} />
    <Image source={require('../../img/3.jpg')} style={styles.bannerImg} />
    <Image source={require('../../img/4.jpg')} style={styles.bannerImg} />
    <Image source={require('../../img/3.jpg')} style={styles.bannerImg} />
</Swiper>

样式:

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    backgroundColor: '#fff',
  },
  bannerImg: {
    height: width*40/75,
    width: width,
  },
  wrapper: {
    width: width,
  },
  paginationStyle: {
    bottom: 6,
  },
  dotStyle: {
    width: 22,
    height: 3,
    backgroundColor:'#fff',
    opacity: 0.4,
    borderRadius: 0,
  },
  activeDotStyle: {
    width: 22,
    height: 3,
    backgroundColor:'#fff',
    borderRadius: 0,
  },
});

三、解决不显示问题

轮播图放在APP的首页,同样有不显示的问题,解决办法和上一篇的办法几乎一样,可以看一下上一篇或是完整代码,这里就不再赘述。

这里和上一篇相比有两处不一样,需要说一下。

1、真正调用接口加载图片的时候,不会出现一开始图片不显示的问题。

2、在状态为false的时候,先显示第一张图片

if (this.state.swiperShow) {
  return (
   <Swiper >
     …………略
   </Swiper>
  )
} else {
  return (
   <View style={{ height: width*40/75 }}>
     <Image source={require('../../img/1.jpg')} style={styles.bannerImg} />
   </View>
  );
}

关于React Native 0.52实现轮播图效果问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注风纳云行业资讯频道了解更多相关知识。

另外有需要云服务器可以了解下风纳云fengnayun.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场(仅限教程资讯栏目),内容著作权归作者所有。转载内容仅用于参考、学习,无商业用途。由于互联网信息复制性强我们难以联系到全部原著作人,若页面内容侵犯了您的权益,我们深感抱歉,还请联系邮箱service@fengnayun.com我们将在第一时间按照著作人要求处理内容。
/template/Home/Dawn/PC/Static

选择风纳云,也许是您成就一番大事业的开端

注册账号