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 相关文章推荐
JQuery 操作/获取table具体代码
Jun 13 Javascript
一个支付页面DEMO附截图
Jul 22 Javascript
AngularJS中directive指令使用之事件绑定与指令交互用法示例
Nov 22 Javascript
基于javascript的Form表单验证
Dec 29 Javascript
AngularJS 霸道的过滤器小结
Apr 26 Javascript
Vue 组件封装 并使用 NPM 发布的教程
Sep 30 Javascript
Angular父子组件通过服务传参的示例方法
Oct 31 Javascript
vue+SSM实现验证码功能
Dec 07 Javascript
vue基础之事件v-onclick=&quot;函数&quot;用法示例
Mar 11 Javascript
js JSON.stringify()基础详解
Jun 19 Javascript
小程序Request的另类用法详解
Aug 09 Javascript
js实现简单五子棋游戏
May 28 Javascript
在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
ThinkPHP整合百度Ueditor图文教程
2014/10/21 PHP
thinkPHP5.0框架独立配置与动态配置方法
2017/03/17 PHP
thinkPHP5实现数据库添加内容的方法
2017/10/25 PHP
JavaScript脚本性能优化注意事项
2008/11/18 Javascript
js chrome浏览器判断代码
2010/03/28 Javascript
ECharts仪表盘实例代码(附源码下载)
2016/02/18 Javascript
canvas绘制万花筒效果(代码分享)
2017/01/20 Javascript
AngularJS ng-repeat指令中使用track by子语句解决重复数据遍历错误问题
2017/01/21 Javascript
微信小程序 点击控件后选中其它反选实例详解
2017/02/21 Javascript
图片加载完成再执行事件的实例
2017/11/16 Javascript
vue如何根据网站路由判断页面主题色详解
2018/11/02 Javascript
微信小程序实现时间预约功能
2018/11/27 Javascript
原生JS forEach()和map()遍历的区别、兼容写法及jQuery $.each、$.map遍历操作
2019/02/27 jQuery
基于elementUI实现图片预览组件的示例代码
2019/03/31 Javascript
js实现头像上传并且可预览提交
2020/12/25 Javascript
[32:47]完美世界DOTA2联赛 GXR vs IO 第二场 11.07
2020/11/09 DOTA
Python实现对比不同字体中的同一字符的显示效果
2015/04/23 Python
Python高级特性切片(Slice)操作详解
2018/09/27 Python
详解Numpy中的数组拼接、合并操作(concatenate, append, stack, hstack, vstack, r_, c_等)
2019/05/27 Python
Python 分享10个PyCharm技巧
2019/07/13 Python
python3 中的字符串(单引号、双引号、三引号)以及字符串与数字的运算
2019/07/18 Python
Linux下通过python获取本机ip方法示例
2019/09/06 Python
Windows下Anaconda安装、换源与更新的方法
2020/04/17 Python
Django ForeignKey与数据库的FOREIGN KEY约束详解
2020/05/20 Python
Python 中如何使用 virtualenv 管理虚拟环境
2021/01/21 Python
全球游戏Keys和卡片市场:GamesDeal
2018/03/28 全球购物
StubHub美国:购买或出售您的门票
2019/07/09 全球购物
J2SDK1.5与J2SDK5.0有什么区别
2012/09/19 面试题
葡萄牙语专业个人求职信
2013/12/10 职场文书
岗位职责风险点
2014/03/12 职场文书
听课评语大全
2014/04/30 职场文书
个人工作表现自我评价
2015/03/06 职场文书
2015年幼儿园个人工作总结
2015/04/25 职场文书
docker compose 部署 golang 的 Athens 私有代理问题
2022/04/28 Servers
Nginx的gzip相关介绍
2022/05/11 Servers
Redis主从复制操作和配置详情
2022/09/23 Redis