溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2020-09-01 11:52:08 來源:腳本之家 閱讀:223 作者:sunshinezhong 欄目:web開發(fā)

首先創(chuàng)建簡單的react-native項目,創(chuàng)建一個文件夾。然后用命令符輸入

react-native init swiper

創(chuàng)建完成之后開發(fā)項目,我用的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里面創(chuàng)建個components文件夾下邊創(chuàng)建個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} //輪播秒數(shù)
   >
    {this.props.data.map((item, idx) => this._renderSwiperItem(item, idx))} //如果數(shù)據(jù)是個對象里面的數(shù)組加一個循環(huán)
   </RNSwiper>
  );
 }
}

這是index.js文件

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

export { Carousel };

公共組件庫

這里用于放置與業(yè)務(wù)無關(guān)的公共組件。組件實現(xiàn)必須考慮靈活性,擴展性,不能包含具體的業(yè)務(wù)邏輯。

組件必須以 你做的業(yè)務(wù)命名 為前綴,如 TryCarousel.js 。每個組件必須單獨放在目錄中,目錄必須全小寫(中橫線分割),如 carousel/TryCarousel.js 。

一個基本的組件結(jié)構(gòu):

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(輪播組件)

主要用于通用的圖片輪播,能夠提供點擊事件響應(yīng)。

Usage:

Props:

屬性 描述 類型 默認值
data Carousel數(shù)據(jù)源 Array -
height Carousel的高度 number 150
onPressItem 點擊Carousel Item的時候觸發(fā) fn -
renderItem 具體的渲染Item的方法,請參考FlatList fn -
autoplay 是否自動切換 bool true
autoplayTimeout Item自動切換的時間間隔(單位s) number 2.5

需要導入的地方

import { HigoCarousel } from '../../components';
<Carousel
      data={} //接受的數(shù)據(jù)
      onPressItem={} //點擊事件
      height={} //圖片高度
      autoplay={} //是否自動播放
      autoplayTimeout={} //過渡時間
      renderItem={item => {
       return <Image source={{ uri: item.imageSource }} style={{ flex: 1 }} />;
      }} //圖片
/>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI