react-native-swiper README

The project's README from github.com/leecade/react-native-swiper. As one markdown file: README.md.

react-native-swiper

The best Swiper component for React Native.

JavaScript Style Guide npm version npm version PR Stats Issue Stats Join the chat

react-native-swiper

Roadmap

see: ROADMAP.md

Changelogs

see more: CHANGELOG.md

Show Cases

Try these cases by yourself very easy, Just open examples/ios/swiper.xcodeproj in Xcode, then press Cmd + R; you may edit examples/index.ios.js for switch cases.

examples/components/Basic

examples/components/Swiper

examples/components/SwiperNumber

examples/components/Phone

examples/components/LoadMinimal

Getting Started

Installation

v1.5.14

$ npm i react-native-swiper --save

v1.6.0-rc

npm i --save react-native-swiper@next

Basic Usage

$ npm i react-native-cli -g
$ react-native init myproject
import React, { Component } from 'react'
import { AppRegistry, StyleSheet, Text, View } from 'react-native'

import Swiper from 'react-native-swiper'

const styles = StyleSheet.create({
  wrapper: {},
  slide1: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#9DD6EB'
  },
  slide2: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#97CAE5'
  },
  slide3: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#92BBD9'
  },
  text: {
    color: '#fff',
    fontSize: 30,
    fontWeight: 'bold'
  }
})

export default class SwiperComponent extends Component {
  render() {
    return (
      <Swiper style={styles.wrapper} showsButtons={true}>
        <View style={styles.slide1}>
          <Text style={styles.text}>Hello Swiper</Text>
        </View>
        <View style={styles.slide2}>
          <Text style={styles.text}>Beautiful</Text>
        </View>
        <View style={styles.slide3}>
          <Text style={styles.text}>And simple</Text>
        </View>
      </Swiper>
    )
  }
}

AppRegistry.registerComponent('myproject', () => SwiperComponent)

Properties

Basic

Prop Default Type Description
horizontal true bool If true, the scroll view's children are arranged horizontally in a row instead of vertically in a column.
loop true bool Set to false to disable continuous loop mode.
index 0 number Index number of initial slide.
showsButtons false bool Set to true make control buttons visible.
autoplay false bool Set to true enable auto play mode.
onIndexChanged (index) => null func Called with the new index when the user swiped

Custom basic style & content

Prop Default Type Description
width - number If no specify default enable fullscreen mode by flex: 1.
height - number If no specify default fullscreen mode by flex: 1.
style {...} style See default style in source.
containerStyle {...} style See default container style in source.
loadMinimal false bool Only load current index slide , loadMinimalSize slides before and after.
loadMinimalSize 1 number see loadMinimal
loadMinimalLoader <ActivityIndicator /> element Custom loader to display when slides aren't loaded

Pagination

Prop Default Type Description
showsPagination true bool Set to true make pagination visible.
paginationStyle {...} style Custom styles will merge with the default styles.
renderPagination - function Complete control how to render pagination with three params (index, total, context) ref to this.state.index / this.state.total / this, For example: show numbers instead of dots.
dot <View style={{backgroundColor:'rgba(0,0,0,.2)', width: 8, height: 8,borderRadius: 4, marginLeft: 3, marginRight: 3, marginTop: 3, marginBottom: 3,}} /> element Allow custom the dot element.
activeDot <View style={{backgroundColor: '#007aff', width: 8, height: 8, borderRadius: 4, marginLeft: 3, marginRight: 3, marginTop: 3, marginBottom: 3,}} /> element Allow custom the active-dot element.
dotStyle - object Allow custom the dot element.
dotColor - string Allow custom the dot element.
activeDotColor - string Allow custom the active-dot element.
activeDotStyle - object Allow custom the active-dot element.

Autoplay

Prop Default Type Description
autoplay true bool Set to true enable auto play mode.
autoplayTimeout 2.5 number Delay between auto play transitions (in second).
autoplayDirection true bool Cycle direction control.

Control buttons

Prop Default Type Description
showsButtons true bool Set to true make control buttons visible.
buttonWrapperStyle {backgroundColor: 'transparent', flexDirection: 'row', position: 'absolute', top: 0, left: 0, flex: 1, paddingHorizontal: 10, paddingVertical: 10, justifyContent: 'space-between', alignItems: 'center'} style Custom styles.
nextButton <Text style={styles.buttonText}>›</Text> element Allow custom the next button.
prevButton <Text style={styles.buttonText}>‹</Text> element Allow custom the prev button.

Props of Children

Prop Default Type Description
style {...} style Custom styles will merge with the default styles.
title {...} element If this parameter is not specified, will not render the title.

Basic props of <ScrollView />

Prop Default Type Description
horizontal true bool If true, the scroll view's children are arranged horizontally in a row instead of vertically in a column.
pagingEnabled true bool If true, the scroll view stops on multiples of the scroll view's size when scrolling. This can be used for horizontal pagination.
showsHorizontalScrollIndicator false bool Set to true if you want to show horizontal scroll bar.
showsVerticalScrollIndicator false bool Set to true if you want to show vertical scroll bar.
bounces false bool If true, the scroll view bounces when it reaches the end of the content if the content is larger then the scroll view along the axis of the scroll direction. If false, it disables all bouncing even if the alwaysBounce* props are true.
scrollsToTop false bool If true, the scroll view scrolls to top when the status bar is tapped.
removeClippedSubviews true bool If true, offscreen child views (whose overflow value is hidden) are removed from their native backing superview when offscreen. This canimprove scrolling performance on long lists.
automaticallyAdjustContentInsets false bool Set to true if you need adjust content insets automation.
scrollEnabled true bool Enables/Disables swiping

@see: http://facebook.github.io/react-native/docs/scrollview.html

Supported ScrollResponder

Prop Params Type Description
onScrollBeginDrag e / state / context function When animation begins after letting up
onMomentumScrollEnd e / state / context function Makes no sense why this occurs first during bounce
onTouchStartCapture e / state / context function Immediately after onMomentumScrollEnd
onTouchStart e / state / context function Same, but bubble phase
onTouchEnd e / state / context function You could hold the touch start for a long time
onResponderRelease e / state / context function When lifting up - you could pause forever before * lifting

Note: each ScrollResponder be injected with two params: state and context, you can get state and context(ref to swiper's this) from params, for example:

var swiper = React.createClass({
  _onMomentumScrollEnd: function (e, state, context) {
    console.log(state, context.state)
  },
  render: function() {
    return (
      <Swiper style={styles.wrapper}
      onMomentumScrollEnd ={this._onMomentumScrollEnd}
     ...
      </Swiper>
    )
  }
})

More ScrollResponder info, see: https://github.com/facebook/react-native/blob/master/Libraries/Components/ScrollResponder.js

Methods

scrollBy(index, animated)

Scroll by relative index.

Parameters:

Name Type default Description
index number undefined offset index
animated bool true offset index

Examples

$ cd examples
$ npm i
$ react-native run-ios

Quick start with examples.

Development

$ cd examples
$ yarn
$ yarn start
$ react-native run-ios

Then launch simulator to preview. Note that you just need to edit the source file src/index.js, the change will auto sync to examples.

After development, you should add test for your modification and make all tests passed to prevent other contributors break the feature in the future accidentally. We use detox + jest for e2e test now, you can read Detox for more detail.

Contribution

Questions

Feel free to contact me or create an issue

Inspired by nolimits4web/Swiper & Design material from Dribbble & made with ♥.


💖 Sponsors

react-native-swiper (leecade/react-native-swiper)

Searching Database…

这是做什么用的?这是一个用于 React Native 应用的轮播(swiper)组件:把任意数量的视图包在里面,用户就可以左右滑动浏览,并且自带循环播放、自动播放、分页圆点和前后翻页按钮。
这是一个完整的 App,还是我加到自己项目里的库?它是一个 npm 库,不是 App:你把 react-native-swiper 安装到现有的 React Native 项目里,然后在需要轮播的地方渲染这个组件就行。
怎么把它加到我的 React Native 应用里?先创建一个 React Native 项目,再从 npm 安装 react-native-swiper(稳定版 v1.5.14 或 v1.6.0-rc),导入 Swiper 并把你的页面作为子元素包进去,例如 ... 。仓库里还附带可以直接运行的示例,可以用 Xcode 打开。
它会拖进来一大堆依赖吗?不会——它在 npm 上唯一的依赖是 prop-types,而且 1.6.0 的候选版本甚至移除了 ViewPagerAndroid,改用 React Native 自带的 ScrollView,所以非常轻量。
稳定版和现在的 React Native 兼容吗?目前的稳定版是 v1.5.14;对 react-native 0.60.x 的支持、修复后的示例以及修正过的 TypeScript 类型都是在 1.6.0-rc 里才加入的,所以如果是新的 RN 项目,你多半应该选这个候选版本。
iOS 和 Android 上都能用吗?可以——它是一个 JavaScript 编写的 React Native 组件,两个平台都能运行:更新日志里提到过 iOS 专属的修复,也提到 Android 上的 ViewPagerAndroid,后者在 1.6.0-rc 中已被 ScrollView 取代。
它能自动播放并不停循环吗?能。循环默认就是开启的,自动播放只需要加一个属性——默认每 2.5 秒切换一次,还可以用 autoplayTimeout 调整间隔、用 autoplayDirection 调整循环方向。
分页的小圆点可以自定义吗?可以——可以通过 dotColor、activeDotColor、dotStyle 和 activeDotStyle 快速调整,也可以完全替换成自定义的 dot/activeDot 元素,或者用 renderPagination(index, total, context) 彻底接管,比如把圆点换成页码。
我怎么在代码里切换页面?调用 scrollBy(index, animated) 可以按相对索引移动;1.6.0-rc 还新增了 scrollTo。底层的 ScrollResponder 回调,比如 onScrollBeginDrag 和 onMomentumScrollEnd,也同样暴露出来了。
它会把每一页都一次性渲染出来吗?不一定:开启 loadMinimal 后,只挂载当前页以及前后各 loadMinimalSize 页(默认 1),其余页面显示自定义的 loadMinimalLoader 元素——对内容很重的页面很有用。
Creator
leecade
Size
8.8 MB
Stars
10.5K
Age
11y
Commits · 90 Days