react-native封装插件swiper的使用方法


Posted in Javascript onMarch 20, 2018

首先创建简单的react-native项目,创建一个文件夹。然后用命令符输入

react-native init swiper

创建完成之后开发项目,我用的vs

react-native封装插件swiper的使用方法

打开控制台,安装swiper依赖。

安装:npm i react-native-swiper --save
查看:npm view react-native-swiper
删除:npm rm react-native-swiper --save
这里还需要 npm i 下更新下本地的依赖库

启动app项目

ios: react-native run-ios
android: react-native run-android

开始上码,在src里面创建个components文件夹下边创建个swiper.js文件,以及index.js,加上说明文档

react-native封装插件swiper的使用方法

import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { StyleSheet, TouchableWithoutFeedback, View } from 'react-native';
import RNSwiper from 'react-native-swiper';

const styles = StyleSheet.create({
 activeDotWrapperStyle: {
  //圆点样式
 },
 activeDotStyle: {
  //圆点样式
 },
 dotStyle: {
  //圆点样式
 }
});

const activeDot = (
 <View style={styles.activeDotWrapperStyle}>
  <View style={styles.activeDotStyle} />
 </View>
);
const dot = <View style={styles.dotStyle} />;

export class Carousel extends Component {
 // Define component prop list
 static propTypes = {
  data: PropTypes.array,
  height: PropTypes.number,
  onPressItem: PropTypes.func,
  renderItem: PropTypes.func.isRequired,
  autoplay: PropTypes.bool,
  autoplayTimeout: PropTypes.number
 };

 // Define props default value
 static defaultProps = {
  data: [],
  height: 150,
  autoplay: true,
  autoplayTimeout: 2.5,
  onPressItem: () => {},
  renderItem: () => {}
 };

 // Define inner state
 state = {
  showSwiper: false
 };

 constructor(props) {
  super(props);
  this.handleItemPress = this.handleItemPress.bind(this);
 }

 componentDidMount() {
  setTimeout(() => {
   this.setState({ showSwiper: true });
  });
 }

 handleItemPress(item) {
  this.props.onPressItem(item);
 }

 _renderSwiperItem(item, index) {
  return (
   <TouchableWithoutFeedback key={index} onPress={() => this.handleItemPress(item)}>
    <View style={[{ flex: 1 }]}>{this.props.renderItem(item)}</View>
   </TouchableWithoutFeedback>
  );
 }

 render() {
  return this.props.data.length === 0 || !this.state.showSwiper ? null : (
   <RNSwiper
    height={this.props.height} //图片高度
    activeDot={activeDot} 
    dot={dot}
    style={{ backgroundColor: '#fff' }}
    autoplay={this.props.autoplay} //是否自动轮播
    autoplayTimeout={this.props.autoplayTimeout} //轮播秒数
   >
    {this.props.data.map((item, idx) => this._renderSwiperItem(item, idx))} //如果数据是个对象里面的数组加一个循环
   </RNSwiper>
  );
 }
}

这是index.js文件

import { Carousel } from './carousel/Carousel';

export { Carousel };

公共组件库

这里用于放置与业务无关的公共组件。组件实现必须考虑灵活性,扩展性,不能包含具体的业务逻辑。

组件必须以 你做的业务命名 为前缀,如 TryCarousel.js 。每个组件必须单独放在目录中,目录必须全小写(中横线分割),如 carousel/TryCarousel.js 。

一个基本的组件结构:

import PropTypes from 'prop-types';
import React, { Component } from 'react';

export class TryCarousel extends Component {
 // Define component prop list
 static propTypes = {};

 // Define props default value
 static defaultProps = {};

 // Define inner state
 state = {};

 constructor(props) {
  super(props);
 }

 // LifeCycle Hooks

 // Prototype Functions

 // Ensure the latest function is render
 render() {}
}

组件列表

carousel(轮播组件)

主要用于通用的图片轮播,能够提供点击事件响应。

Usage:

Props:

属性 描述 类型 默认值
data Carousel数据源 Array -
height Carousel的高度 number 150
onPressItem 点击Carousel Item的时候触发 fn -
renderItem 具体的渲染Item的方法,请参考FlatList fn -
autoplay 是否自动切换 bool true
autoplayTimeout Item自动切换的时间间隔(单位s) number 2.5

需要导入的地方

import { HigoCarousel } from '../../components';
<Carousel
      data={} //接受的数据
      onPressItem={} //点击事件
      height={} //图片高度
      autoplay={} //是否自动播放
      autoplayTimeout={} //过渡时间
      renderItem={item => {
       return <Image source={{ uri: item.imageSource }} style={{ flex: 1 }} />;
      }} //图片
/>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
IE7中javascript操作CheckBox的checked=true不打勾的解决方法
Dec 07 Javascript
多种方式实现JS调用后台方法进行数据交互
Aug 20 Javascript
js中的replace方法使用介绍
Oct 28 Javascript
HTML5之WebSocket入门3 -通信模型socket.io
Aug 21 Javascript
深入浅析jQuery对象$.html
Aug 22 Javascript
JavaScript事件用法浅析
Oct 31 Javascript
浅谈jQuery的bind和unbind事件(绑定和解绑事件)
Mar 02 Javascript
解决layui checkbox 提交多个值的问题
Sep 02 Javascript
React路由鉴权的实现方法
Sep 05 Javascript
Node.js控制台彩色输出的方法与原理实例详解
Dec 01 Javascript
小程序组件传值和引入sass的方法(使用vant Weapp组件库)
Nov 24 Javascript
vue实现拖拽进度条
Mar 01 Vue.js
在vue项目中使用sass的配置方法
Mar 20 #Javascript
webpack vue项目开发环境局域网访问方法
Mar 20 #Javascript
动态加载、移除js/css文件的示例代码
Mar 20 #Javascript
webpack 打包压缩js和css的方法示例
Mar 20 #Javascript
浅谈Node 调试工具入门教程
Mar 20 #Javascript
使用Vue.js开发微信小程序开源框架mpvue解析
Mar 20 #Javascript
浅谈jquery fullpage 插件增加头部和版权的方法
Mar 20 #jQuery
You might like
使用 MySQL Date/Time 类型
2008/03/26 PHP
php解析http获取的json字符串变量总是空白null
2015/03/02 PHP
Smarty模板变量调节器用法分析
2016/05/23 PHP
PHP 出现 http500 错误的解决方法
2021/03/09 PHP
jQuery+jqmodal弹出窗口实现代码分明
2010/06/14 Javascript
jquery选择器(常用选择器说明)
2010/09/28 Javascript
javascript中用星号表示预录入内容的实现代码
2011/01/08 Javascript
读JavaScript DOM编程艺术笔记
2011/11/15 Javascript
jquery中map函数与each函数的区别实例介绍
2014/06/23 Javascript
JavaScript 学习笔记之操作符(续)
2015/01/14 Javascript
jQuery网页版打砖块小游戏源码分享
2015/08/20 Javascript
AngularJS在IE下取数据总是缓存问题的解决方法
2016/08/05 Javascript
jQuery Ajax File Upload实例源码
2016/12/12 Javascript
JavaScript数据类型的存储方法详解
2017/08/25 Javascript
JavaScript Math对象和调试程序的方法分析
2019/05/13 Javascript
列举Python中吸引人的一些特性
2015/04/09 Python
python获取从命令行输入数字的方法
2015/04/29 Python
Python的Flask框架中SQLAlchemy使用时的乱码问题解决
2015/11/07 Python
十个Python程序员易犯的错误
2015/12/15 Python
Python实现自定义函数的5种常见形式分析
2018/06/16 Python
kaggle+mnist实现手写字体识别
2018/07/26 Python
Python单向链表和双向链表原理与用法实例详解
2018/08/31 Python
使用Python抓取豆瓣影评数据的方法
2018/10/17 Python
Python 3.6 -win64环境安装PIL模块的教程
2019/06/20 Python
通过字符串导入 Python 模块的方法详解
2019/10/27 Python
通用的Django注册功能模块实现方法
2021/02/05 Python
css3绘制天猫logo实现代码
2012/11/06 HTML / CSS
应聘编辑职位自荐信范文
2014/01/05 职场文书
浙江文明网签名寄语
2014/01/18 职场文书
大学生职业规划书的范本
2014/02/18 职场文书
运动会闭幕式解说词
2014/02/21 职场文书
教职工代表大会主持词
2014/04/01 职场文书
销售顾问工作计划书
2014/09/15 职场文书
个人收入证明模板
2014/09/18 职场文书
中学生检讨书1000字
2014/10/28 职场文书
重阳节活动主持词
2015/07/04 职场文书