React Native-图片轮播

来源:互联网 发布:兔小将是什么 知乎 编辑:程序博客网 时间:2024/04/28 13:15

图片轮播在App开发中经常使用,这里图片轮播使用的是第三方组件react-native-swiper,
我们启动npm命令行,在项目的根目录使用如下命令安装模块。

$ npm install react-native-swiper --save$ npm i react-timer-mixin --save

安装好之后 开始写代码,

1,引入第三方组件

var Swiper = require('react-native-swiper');

这样我们在项目中就可以直接使用Swiper组件了。

2,准备好轮播图片的资源,

var images=[    'http://ac-c6scxa78.clouddn.com/f6b64dc4bf7bee56.jpg',    'http://ac-c6scxa78.clouddn.com/91ead58b0bb213b6.jpg',    'http://ac-c6scxa78.clouddn.com/d67316858f6c71f3.jpg',    'http://ac-c6scxa78.clouddn.com/c81c5b7be1838a1e.jpg',    'http://ac-c6scxa78.clouddn.com/54fe022399902788.jpg',];

3

render() {        return (            <Swiper height={200}                >                {this.renderImg()}            </Swiper>        );    }    renderImg(){        var imageViews=[];        for(var i=0;i<images.length;i++){            imageViews.push(                <Image                    key={i}                    style={{flex:1}}                    source={{uri:images[i]}}                    />            );        }        return imageViews;    }

效果如下:
这里写图片描述
但是我们发现原点的位置不是我们想要的,那么如何改变原点的位置 以及颜色呢?

 <Swiper height={200}     paginationStyle={{bottom:10}}     autoplay={true}     dot={<View style={{width:8,height:8,backgroundColor:'white',borderRadius:4,marginLeft:3,marginRight:3}}></View>}     activeDot={<View style={{width:8,height:8,backgroundColor:'orange',borderRadius:4,marginLeft:3,marginRight:3}}></View>}                >      {this.renderImg()} </Swiper>

paginationStyle设置点的样式,
dot和activeDot设置选中和未选中的样式

这样我们就可以将轮播点的位置设置成我们想要的效果了,
IOS
这里写图片描述
Android
这里写图片描述
还有很多比较好的样式 请参考https://github.com/leecade/react-native-swiper

0 1
原创粉丝点击