react native 第三方组件react-native-swiper 轮播组件

github地址:https://github.com/leecade/react-native-swiper

使用方法:安装:npm i react-native-swiper –save

查看模块:npm view react-native-swiper 
删除模块:npm rm react-native-swiper –save (这个添加save会在删除的同时去除package.json中的依赖) 
查看帮助命令:npm help 命令 (例如npm help -i查看i的使用)

基本用法

import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    Image,
    TouchableOpacity,
    ViewPagerAndroid,
    Navigator,
    View
    } from 'react-native';

import Swiper from 'react-native-swiper';

class hello extends Component {
  render() {
    return (
      <Swiper style={styles.wrapper} showsButtons={true}>
        <View style={styles.slide1}>
          <Text style={styles.text}>Hello Swiper</Text>
        </View>
        <View style={styles.slide2}>
          <Text style={styles.text}>Beautiful</Text>
        </View>
        <View style={styles.slide3}>
          <Text style={styles.text}>And simple</Text>
        </View>
      </Swiper>
    );
  }
}

const styles = StyleSheet.create({
  wrapper: {
  },
  slide1: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#9DD6EB',
  },
  slide2: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#97CAE5',
  },
  slide3: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#92BBD9',
  },
  text: {
    color: '#fff',
    fontSize: 30,
    fontWeight: 'bold',
  }
});


AppRegistry.registerComponent('hello', () => hello);

 效果:

详细属性

接下来让我们好好探索一下这个框架的基本属性:

基本属性:

PropDefaultTypeDescription
horizontaltrueboolean为false提示小圆点在侧面
looptrueboolean设置为false以禁用连续循环模式
index0int默认显示第几页
showsButtonsfalseint设置为true显示button
autoplayfalseboolean设置为true将启用自动播放模式。
下面演示一下下面这些样式的效果 我设置默认选择第二页,显示button,小圆点在最下面,禁用无限循环。
<Swiper style={styles.wrapper} showsButtons={true} horizontal={true} loop={false} index={1}>
        <View style={styles.slide1}>
          <Text style={styles.text}>我是第一页</Text>
        </View>
        <View style={styles.slide2}>
          <Text style={styles.text}>我是第二页</Text>
        </View>
        <View style={styles.slide3}>
          <Text style={styles.text}>我是第三页</Text>
        </View>
      </Swiper>

 

自定义基本样式

PropDefaultTypeDescription
width-/-number默认flex:1
height-/-number默认flex:1
style{…}style请参阅源中的默认样式。
loadMinimalfalseboolean只加载当前索引幻灯片
loadMinimalSize1number请参阅loadMinimal
loadMinimalLoader《ActivityIndicator/》element在未加载幻灯片时显示自定义加载程序
设置宽高为200,200,loadMinimal为true加载当前索引幻灯片。

<Swiper style={styles.wrapper}
              showsButtons={true}
              horizontal={true} 
              loop={false} 
              index={1}
              loadMinimal={true}>
可以看出宽高都有了变化 而且只加载了一个 
视图,其他的都是空白的 
当我们把loadMinimal设置为true同时,loadMinimalSize设置为3这时候就回复正常了,让我们看一下效果:

<Swiper style={styles.wrapper}
              showsButtons={true}
              horizontal={true} 
              loop={false} 
              index={1}

              loadMinimal={true}
              loadMinimalSize={3}

             >

 

PropDefaultTypeDescription
showsPaginationtrueboolean设置为true可使分页可见
paginationStyle{…}style自定义样式将与默认样式合并
renderPagination-/-function通过三个参数(index, total, context)确定如何渲染
dot《View style={{backgroundColor:’rgba(0,0,0,.2)’, width: 8, height: 8,borderRadius: 4, marginLeft: 3, marginRight: 3, marginTop: 3, marginBottom: 3,}} /》element允许自定义点元素
activeDot《View style={{backgroundColor: ‘#007aff’, width: 8, height: 8, borderRadius: 4, marginLeft: 3, marginRight: 3, marginTop: 3, marginBottom: 3,}} /》element允许自定义active-dot元素

接下来让我们看一个分页的demo: 
先看一下效果:

修改小圆尖头样式

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 */

import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    Image,
    TouchableOpacity,
    ViewPagerAndroid,
    Navigator,
    View,
    Dimensions
    } from 'react-native';

import Swiper from 'react-native-swiper';

const { width } = Dimensions.get('window')

class hello extends Component {
  render() {
    return (
      <View>
        <Swiper style={styles.wrapper} height={200} horizontal={true} autoplay={true}>
          <View style={styles.slide1}>
            <Text style={styles.text}>第一页</Text>
          </View>
          <View style={styles.slide2}>
            <Text style={styles.text}>第二页</Text>
          </View>
          <View style={styles.slide3}>
            <Text style={styles.text}>第三页</Text>
          </View>
        </Swiper>

        <Swiper style={styles.wrapper} height={240}
          dot={<View style={{backgroundColor: 'rgba(0,0,0,.2)', width: 5, height: 5, borderRadius: 4, marginLeft: 3, marginRight: 3, marginTop: 3, marginBottom: 3}} />}
          activeDot={<View style={{backgroundColor: '#000', width: 8, height: 8, borderRadius: 4, marginLeft: 3, marginRight: 3, marginTop: 3, marginBottom: 3}} />}
          paginationStyle={{
            bottom: -23, left: null, right: 10
          }} loop>
          <View style={styles.slide} title={<Text numberOfLines={1}>Aussie tourist dies at Bali hotel</Text>}>
            <Image resizeMode='stretch' style={styles.image} source={require('./imgs/1.jpg')} />
          </View>
          <View style={styles.slide} title={<Text numberOfLines={1}>Big lie behind Nine’s new show</Text>}>
            <Image resizeMode='stretch' style={styles.image} source={require('./imgs/2.jpg')} />
          </View>
          <View style={styles.slide} title={<Text numberOfLines={1}>Why Stone split from Garfield</Text>}>
            <Image resizeMode='stretch' style={styles.image} source={require('./imgs/3.jpg')} />
          </View>
          <View style={styles.slide} title={<Text numberOfLines={1}>Learn from Kim K to land that job</Text>}>
            <Image resizeMode='stretch' style={styles.image} source={require('./imgs/4.jpg')} />
          </View>
        </Swiper>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  wrapper: {
  },

  slide: {
    flex: 1,
    justifyContent: 'center',
    backgroundColor: 'transparent'
  },

  slide1: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#9DD6EB'
  },

  slide2: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#97CAE5'
  },

  slide3: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#92BBD9'
  },

  text: {
    color: '#fff',
    fontSize: 30,
    fontWeight: 'bold'
  },

  image: {
    width,
    flex: 1
  }
});
AppRegistry.registerComponent('hello', () => hello);

 

 

 

Autoplay自动换图

PropDefaultTypeDescription
autoplaytrueboolean设置为true将启用自动播放模式
autoplayTimeout2.5number延迟时间(秒
autoplayDirectiontrueboolean循环方向控制

 

转载于:https://www.cnblogs.com/allenxieyusheng/p/6836228.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值