首先創(chuàng)建簡單的react-native項目,創(chuàng)建一個文件夾。然后用命令符輸入
react-native init swiper
創(chuàng)建完成之后開發(fā)項目,我用的vs
打開控制臺,安裝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,加上說明文檔
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))} //如果數據是個對象里面的數組加一個循環(huán) </RNSwiper> ); } }
這是index.js文件
import { Carousel } from './carousel/Carousel'; export { Carousel };
公共組件庫
這里用于放置與業(yè)務無關的公共組件。組件實現必須考慮靈活性,擴展性,不能包含具體的業(yè)務邏輯。
組件必須以 你做的業(yè)務命名 為前綴,如 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的時候觸發(fā) | 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 }} />; }} //圖片 />
相信看了本文案例你已經掌握了方法,更多精彩請關注Gxl網其它相關文章!
推薦閱讀:
vue-router內query動態(tài)傳參如何處理
如何操作nodejs對密碼進行加密
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com