基于React Native 0.52實現(xiàn)輪播圖效果
本文基于React Native 0.52
Demo上傳到Git了,有需要可以看看,寫了新內(nèi)容會上傳的(Git地址)
一、總覽
輪播圖幾乎是必備的效果,這里選擇 react-native-swiper 來實現(xiàn),效果如下圖:
二、實現(xiàn)輪播圖效果
1、通過npm安裝react-native-swiper
npm install react-native-swiper --save
2、在recommend.js引入react-native-swiper
import Swiper from 'react-native-swiper';
3、用 react-native-swiper 可以很容易的實現(xiàn)輪播的效果
- showButtons —— 是否顯示左右翻頁按鈕
- autoPlay —— 是否自動播放
- paginationStyle —— 包含小點點的容器的樣式,這里用來調(diào)整位置
- dotStyle —— 小點點的樣式
- activeDotStyle —— 當(dāng)前被激活的小點點的樣式
<Swiper style={styles.wrapper} height={width*40/75} showsButtons={false} autoplay={true} paginationStyle={styles.paginationStyle} dotStyle={styles.dotStyle} activeDotStyle={styles.activeDotStyle} > <Image source={require('../../img/1.jpg')} style={styles.bannerImg} /> <Image source={require('../../img/2.jpg')} style={styles.bannerImg} /> <Image source={require('../../img/3.jpg')} style={styles.bannerImg} /> <Image source={require('../../img/4.jpg')} style={styles.bannerImg} /> <Image source={require('../../img/3.jpg')} style={styles.bannerImg} /> </Swiper>
樣式:
const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', backgroundColor: '#fff', }, bannerImg: { height: width*40/75, width: width, }, wrapper: { width: width, }, paginationStyle: { bottom: 6, }, dotStyle: { width: 22, height: 3, backgroundColor:'#fff', opacity: 0.4, borderRadius: 0, }, activeDotStyle: { width: 22, height: 3, backgroundColor:'#fff', borderRadius: 0, }, });
三、解決不顯示問題
輪播圖放在APP的首頁,同樣有不顯示的問題,解決辦法和上一篇的辦法幾乎一樣,可以看一下上一篇或是完整代碼,這里就不再贅述。
這里和上一篇相比有兩處不一樣,需要說一下。
1、真正調(diào)用接口加載圖片的時候,不會出現(xiàn)一開始圖片不顯示的問題。
2、在狀態(tài)為false的時候,先顯示第一張圖片
if (this.state.swiperShow) { return ( <Swiper > …………略 </Swiper> ) } else { return ( <View style={{ height: width*40/75 }}> <Image source={require('../../img/1.jpg')} style={styles.bannerImg} /> </View> ); }
recommend.js完整代碼 地址
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用webpack5從0到1搭建一個react項目的實現(xiàn)步驟
這篇文章主要介紹了使用webpack5從0到1搭建一個react項目的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12React?Native?加載H5頁面的實現(xiàn)方法
這篇文章主要介紹了React?Native?加載H5頁面的實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04react項目打包后點擊index.html頁面出現(xiàn)空白的問題
這篇文章主要介紹了react項目打包后點擊index.html頁面出現(xiàn)空白的問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06React如何使用create-react-app創(chuàng)建react項目
這篇文章主要介紹了React如何使用create-react-app創(chuàng)建react項目問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03react ant-design Select組件下拉框map不顯示的解決
這篇文章主要介紹了react ant-design Select組件下拉框map不顯示的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03