简单好用的react native轮播图组件——react-native-better-banner

react-native-better-banner

一款用于react native的轮播图组件,支持无缝滚动、定时滚动、指示器显示、标题显示,可轮播图片或自定义组件。欢迎star,项目地址与Demo:https://github.com/tmxiong/better-banner

Installation

npm install --save react-native-better-banner

Usage

import BetterBanner from 'react-native-better-banner'

功能列表

  • 图片轮播
  • 自定义组件轮播
  • 定时轮播
  • 同步显示轮播标题
  • 无缝轮播
  • 指示器定制
  • 点击事件回调
  • 滚动事件回调
  • 支持android与iOS

banner样式1
banner样式2[外链图片转存失败(img-hFbCWDlk-1569150029367)(https://raw.githubusercontent.com/tmxiong/better-banner/master/src/screenshot/screenshot02.png)]

涉及主要知识点

  • 父子组件传值与回调
  • ref
  • setNativeProps
  • onLayout

Props

propstypedefaultnote
bannerImagesArray[]用于展示轮播图片, 与bannerComponents二选一
bannerComponentsArray[]用于展示轮播自定义组件,与bannerImages二选一
bannerHeightNumber250banner的默认高度
bannerTitlesArray[]每张图片或组件对应的标题
bannerTitleTextColorString#fff每张图片或组件对应的标题的文字颜色
bannerTitleTextSizeNumber14每张图片或组件对应的标题的文字大小
scrollIntervalNumber2000自动轮播的时间间隔,单位ms
isAutoScrollBooleantrue是否开启自动轮播
isSeamlessScrollBooleanfalse是否开启无缝滚动(iOS下正常,安卓某些机型可能出现滚动异常)
adaptSeamlessScrollValueBooleanfalse如果开启无缝滚动在某些机型滚动异常,可针对这些机型设置truefalse, 此值实际上是设置是否显示ScrollViewscrollTo的滚动动画
indicatorWidthNumber10指示器宽度
indicatorHeightNumber6指示器高度
indicatorColorStringrgba(255,255,255,0.6)指示器颜色
indicatorStyleObject{}指示器样式,您也可以直接使用此属性一次性设置指示器宽、高、颜色和圆角等,它会覆盖以上indicatorWidth,indicatorHeightindicatorColor属性
indicatorGapNumber6指示器之间的间隔
activeIndicatorColorString#fff活动指示器颜色
indicatorGroupPositionStringright指示器组的位置,可设置left,center,right。如果您设置了bannerTitles,则此属性只能是right
indicatorGroupSideOffsetNumber10指示器组的左右边距
indicatorContainerHeightNumber32指示器容器高度
indicatorContainerBackgroundColorStringtransparent指示器容器背景色
onPress()Function()=>{}点击轮播图后的回调函数,会传回banner的index
onScrollEnd()Function()=>{}滚动完每张轮播图的回调函数,等同于ScrollViewonMomentumScrollEnd

Example01

Use Custom Components
在这里插入图片描述

import React from 'react';
import {
    StyleSheet,
    View,
    Text,
} from 'react-native';

import BetterBanner from 'react-native-better-banner';

const App = () => {
    return (
        <View style={styles.container}>
            <BetterBanner
                bannerComponents={[
                    <View style={{
                        width: '100%',
                        height: '100%',
                        backgroundColor: '#1997fc',
                        alignItems: 'center',
                        justifyContent: 'center'
                    }}>
                        <Text style={{fontSize: 35, color: '#fff', marginBottom: 10}}>Page 01</Text>
                        <Text style={{fontSize: 15, color: '#fff'}}>Welcome! have a good time</Text>
                    </View>,
                    <View style={{
                        width: '100%',
                        height: '100%',
                        backgroundColor: '#da578f',
                        alignItems: 'center',
                        justifyContent: 'center'
                    }}>
                        <Text style={{fontSize: 35, color: '#fff', marginBottom: 10}}>Page 02</Text>
                        <Text style={{fontSize: 15, color: '#fff'}}>Welcome! have a good time</Text>
                    </View>,
                    <View style={{
                        width: '100%',
                        height: '100%',
                        backgroundColor: '#7c3fe4',
                        alignItems: 'center',
                        justifyContent: 'center'
                    }}>
                        <Text style={{fontSize: 35, color: '#fff', marginBottom: 10}}>Page 03</Text>
                        <Text style={{fontSize: 15, color: '#fff'}}>Welcome! have a good time</Text>
                    </View>,
                ]}
                bannerTitles={["Page 01 Page 01 Page 01 Page 01 Page 01 Page 01 Page 01 ", "Page 02", "Page 03"]}
                onPress={(index) => alert('you pressed index is : ' + index)}
                indicatorContainerBackgroundColor={'rgba(0,0,0,0.3)'}
                isSeamlessScroll={true}
            />
        </View>
    );
};

const styles = StyleSheet.create({
   container: {
       flex: 1
   }

});

export default App;

Example 02

Use images
在这里插入图片描述

import React from 'react';
import {
    StyleSheet,
    View,
    Text,
} from 'react-native';

import BetterBanner from 'react-native-better-banner';

const App = () => {
    return (
        <View style={styles.container}>
            <BetterBanner
                bannerImages={[
                    {uri: "http://attachments.gfan.net.cn/forum/attachments2/201402/05/211339r5eizavo2g5efuar.jpg.thumb.jpg"},
                    {uri: "http://attachments.gfan.net.cn/forum/attachments2/201402/05/211342pkd8axiibj5axizi.jpg.thumb.jpg"},
                    {uri: "http://attachments.gfan.net.cn/forum/attachments2/201402/05/2113441e4dw5zvv49jkdow.jpg.thumb.jpg"},
                    {uri: "http://attachments.gfan.net.cn/forum/attachments2/201402/05/2113472s3y3y1w62n2io6y.jpg.thumb.jpg"},
                ]}
                onPress={(index) => alert('you pressed index is : ' + index)}
                isSeamlessScroll={true}
                indicatorGroupPosition={'center'}
            />
        </View>
    );
};

const styles = StyleSheet.create({
   container: {
       flex: 1
   }

});

export default App;

欢迎star,项目地址与Demo:https://github.com/tmxiong/better-banner

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
React Native提供了一些用于缩放图片的组件,其中之一是`react-native-safe-area-context`库中的`SafeAreaImage`组件。这个组件可以帮助你在设备边缘安全区域内展示图片,并允许你进行缩放操作。 要使用`SafeAreaImage`组件进行图片缩放,你可以按照以下步骤进行操作: 1. 首先,确保你已经安装了`react-native-safe-area-context`库。你可以使用npm或yarn进行安装: ```bash npm install react-native-safe-area-context ``` 或 ```bash yarn add react-native-safe-area-context ``` 2. 在你的React Native应用程序中引入`SafeAreaImage`组件: ```javascript import SafeAreaImage from 'react-native-safe-area-context'; ``` 3. 在需要展示缩放图片的组件中使用`SafeAreaImage`组件,并设置相关属性。例如,你可以使用`style`属性设置图片的尺寸和位置,使用`resizeMode`属性设置图片的缩放模式,以及使用`source`属性设置图片的来源。 ```javascript <SafeAreaImage style={{ width: 200, height: 200 }} resizeMode="center" source={require('./path/to/your/image.png')} /> ``` 在上面的示例中,我们设置了图片的宽度和高度为200像素,并使用`resizeMode="center"`将图片居中显示。我们还通过`source`属性指定了图片的来源路径。 需要注意的是,上述示例中的路径是一个相对路径,你需要根据实际情况修改为你的图片文件的实际路径。另外,你还可以根据需要设置其他样式和属性来定制缩放图片的外观和行为。 除了`react-native-safe-area-context`库提供的`SafeAreaImage`组件React Native还提供了其他一些用于缩放图片的组件和第三方库,如`react-native-responsive-image`、`react-native-safe-area-image`等。你可以根据需要选择适合你的库或组件来进行图片缩放操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值