2.0.18 • Published 7 years ago

@eimagine/react-native-swiper v2.0.18

Weekly downloads
-
License
ISC
Repository
bitbucket
Last release
7 years ago

react-native-swiper

npm version npm version Issue Stats Issue Stats Join the chat at https://gitter.im/leecade/react-native-swiper

logo

The best Swiper component for React Native.

Roadmap

V2

  • Improve performance

  • Landscape mode

Please track in milestones:v2

Contribute code to branch:Android and branch:V2

Provide creative in issues


V1

  • Support for Android

  • More switch effects

  • Unit tests

  • Check typo

  • Fix bugs

  • Infinite loop

  • Direction control

  • Complete custom style

  • Allow title display & custom

  • Multiple instances

  • Custom container size

  • Control buttons

  • Autoplay

  • Custom pagination style

  • State inject

Changelogs

  • 1.4.9

    • Adds extra check for title
  • 1.4.8

    • Fixes loop and autoplay bugs in android
  • 1.4.7

    • Don't attempt to mutate this.props
    • Fixes examples links
    • Adds drag end handling to always reset state.isScrolling
    • Fixes float calculation error
  • 1.4.6

    • refactors examples
    • prevents mutation of this.props
    • fixes android index loop issue
  • 1.4.5

    • renames scrollTo() to scrollBy()
    • image index is now always an integer
    • prevents parent state updates from reseting index counter
    • fixes issue with scrolling not working sometimes
  • 1.4.4

    • Support for React Native latest(^0.26.0)
    • Updates examples to work with react-native-swiper 1.4.4
    • Examples now run on Android (some are still buggy, needs more work)
    • Removes old examples folder
  • 1.4.3

    • Fixed auto play issue when only a child view
  • v1.4.0

    • Support for React Native latest(^0.17.0) & Upgrade examples base on Xcode 7.2 / React Native 0.17.0

    • Upgrade build tool to babel6

    • Add missing deps react-timer-mixin

    • 1f8643a Move Dimensions import to deconstructed React import for react-native (@thanks @jedrekk).

    • e28af9b Support index property in combination with loop property (@thanks @almost).

    • 6c832d fix warnings about keys for dots (@thanks @sunnylqm).

    • 8de1afc Changes to make the example work in xcode 7 (@thanks @allomov).

  • v1.3.0

  • v1.2.2

    • 890c0ce ensure onMomentumScrollEnd synchronous update index.
  • v1.2.0

    • 838b24c Re-design a loop model for avoid img flickering.
    • 9cb91c5 fixes #7 onMomentumScrollEnd lost event param. (thanks @smothers)
  • v1.1.1

  • v1.1.0

  • v1.0.4

  • v1.0.3

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/samples/basic.js

![](http://i.imgur.com/zrsazAG.gif =300x)

examples/samples/swiper.js

![](http://i.imgur.com/hP3f3oO.gif =300x)

examples/samples/swiper_number.js

![](http://i.imgur.com/0rqESVb.gif =300x)

examples/samples/phone.js

![](http://i.imgur.com/c1BhjZm.gif =300x)

Getting Started

Installation

$ npm i react-native-swiper --save

Basic Usage

  • Install react-native first
$ npm i react-native -g
  • Initialization of a react-native project
$ react-native init myproject
  • Then, edit myproject/index.ios.js, like this:
var Swiper = require('react-native-swiper')
// es6
// import Swiper from 'react-native-swiper'

var React = require('react-native');
var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
} = React;

var 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',
  }
})

var swiper = React.createClass({
  render: function() {
    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('swiper', () => swiper)

Properties

Basic

PropDefaultTypeDescription
horizontaltrueboolIf true, the scroll view's children are arranged horizontally in a row instead of vertically in a column.
looptrueboolSet to false to disable continuous loop mode.
index0numberIndex number of initial slide.
showsButtonsfalseboolSet to true make control buttons visible.
autoplayfalseboolSet to true enable auto play mode.

Custom basic style & content

PropDefaultTypeDescription
width-numberIf no specify default enable fullscreen mode by flex: 1.
height-numberIf no specify default fullscreen mode by flex: 1.
style{...}styleSee default style in source.

Pagination

PropDefaultTypeDescription
showsPaginationtrueboolSet to true make pagination visible.
paginationStyle{...}styleCustom styles will merge with the default styles.
renderPagination-functionComplete 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,}} />elementAllow custom the dot element.
activeDot<View style={{backgroundColor: '#007aff', width: 8, height: 8, borderRadius: 4, marginLeft: 3, marginRight: 3, marginTop: 3, marginBottom: 3,}} />elementAllow custom the active-dot element.

Autoplay

PropDefaultTypeDescription
autoplaytrueboolSet to true enable auto play mode.
autoplayTimeout2.5numberDelay between auto play transitions (in second).
autoplayDirectiontrueboolCycle direction control.

Control buttons

PropDefaultTypeDescription
showsButtonstrueboolSet 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'}styleCustom styles.
nextButton<Text style={styles.buttonText}>›</Text>elementAllow custom the next button.
prevButton<Text style={styles.buttonText}>‹</Text>elementAllow custom the prev button.

Props of Children

PropDefaultTypeDescription
style{...}styleCustom styles will merge with the default styles.
title{...}elementIf this parameter is not specified, will not render the title.

Basic props of <ScrollView />

PropDefaultTypeDescription
horizontaltrueboolIf true, the scroll view's children are arranged horizontally in a row instead of vertically in a column.
pagingEnabledtrueboolIf true, the scroll view stops on multiples of the scroll view's size when scrolling. This can be used for horizontal pagination.
showsHorizontalScrollIndicatorfalseboolSet to true if you want to show horizontal scroll bar.
showsVerticalScrollIndicatorfalseboolSet to true if you want to show vertical scroll bar.
bouncesfalseboolIf 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.
scrollsToTopfalseboolIf true, the scroll view scrolls to top when the status bar is tapped.
removeClippedSubviewstrueboolIf 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.
automaticallyAdjustContentInsetsfalseboolSet to true if you need adjust content insets automation.

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

Supported ScrollResponder

PropParamsTypeDescription
onScrollBeginDrage / state / contextfunctionWhen animation begins after letting up
onMomentumScrollEnde / state / contextfunctionMakes no sense why this occurs first during bounce
onTouchStartCapturee / state / contextfunctionImmediately after onMomentumScrollEnd
onTouchStarte / state / contextfunctionSame, but bubble phase
onTouchEnde / state / contextfunctionYou could hold the touch start for a long time
onResponderReleasee / state / contextfunctionWhen 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)

Scroll by relative index.

Parameters:

NameTypeDescription
indexnumberoffset index

Examples

@TODO, see code in examples first.

Development

$ npm start

Contribution

Questions

Feel free to contact me or create an issue

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

2.0.18

7 years ago