1.0.5 • Published 4 years ago

react-native-swipe-gestures v1.0.5

Weekly downloads
117,168
License
MIT
Repository
github
Last release
4 years ago

react-native-swipe-gestures

React Native component for handling swipe gestures in up, down, left and right direction.

Installation

npm i -S react-native-swipe-gestures

Usage

'use strict';

import React, {Component} from 'react';
import {View, Text} from 'react-native';
import GestureRecognizer, {swipeDirections} from 'react-native-swipe-gestures';

class SomeComponent extends Component {

  constructor(props) {
    super(props);
    this.state = {
      myText: 'I\'m ready to get swiped!',
      gestureName: 'none',
      backgroundColor: '#fff'
    };
  }

  onSwipeUp(gestureState) {
    this.setState({myText: 'You swiped up!'});
  }

  onSwipeDown(gestureState) {
    this.setState({myText: 'You swiped down!'});
  }

  onSwipeLeft(gestureState) {
    this.setState({myText: 'You swiped left!'});
  }

  onSwipeRight(gestureState) {
    this.setState({myText: 'You swiped right!'});
  }

  onSwipe(gestureName, gestureState) {
    const {SWIPE_UP, SWIPE_DOWN, SWIPE_LEFT, SWIPE_RIGHT} = swipeDirections;
    this.setState({gestureName: gestureName});
    switch (gestureName) {
      case SWIPE_UP:
        this.setState({backgroundColor: 'red'});
        break;
      case SWIPE_DOWN:
        this.setState({backgroundColor: 'green'});
        break;
      case SWIPE_LEFT:
        this.setState({backgroundColor: 'blue'});
        break;
      case SWIPE_RIGHT:
        this.setState({backgroundColor: 'yellow'});
        break;
    }
  }

  render() {

    const config = {
      velocityThreshold: 0.3,
      directionalOffsetThreshold: 80
    };

    return (
      <GestureRecognizer
        onSwipe={(direction, state) => this.onSwipe(direction, state)}
        onSwipeUp={(state) => this.onSwipeUp(state)}
        onSwipeDown={(state) => this.onSwipeDown(state)}
        onSwipeLeft={(state) => this.onSwipeLeft(state)}
        onSwipeRight={(state) => this.onSwipeRight(state)}
        config={config}
        style={{
          flex: 1,
          backgroundColor: this.state.backgroundColor
        }}
        >
        <Text>{this.state.myText}</Text>
        <Text>onSwipe callback received gesture: {this.state.gestureName}</Text>
      </GestureRecognizer>
    );
  }
}

export default SomeComponent;

Config

Can be passed within optional config property.

ParamsTypeDefaultDescription
velocityThresholdNumber0.3Velocity that has to be breached in order for swipe to be triggered (vx and vy properties of gestureState)
directionalOffsetThresholdNumber80Absolute offset that shouldn't be breached for swipe to be triggered (dy for horizontal swipe, dx for vertical swipe)
gestureIsClickThresholdNumber5Absolute distance that should be breached for the gesture to not be considered a click (dx or dy properties of gestureState)

Methods

onSwipe(gestureName, gestureState)

ParamsTypeDescription
gestureNameStringName of the gesture (look example above)
gestureStateObjectgestureState received from PanResponder

onSwipeUp(gestureState)

ParamsTypeDescription
gestureStateObjectgestureState received from PanResponder

onSwipeDown(gestureState)

ParamsTypeDescription
gestureStateObjectgestureState received from PanResponder

onSwipeLeft(gestureState)

ParamsTypeDescription
gestureStateObjectgestureState received from PanResponder

onSwipeRight(gestureState)

ParamsTypeDescription
gestureStateObjectgestureState received from PanResponder
qmodalframework-reactnative-component-libreact-native-in-app-notification-fork@chzappsinc/demo-1023dictionarymacmillanreact-native-calendarsordering-ui-react-native@everything-registry/sub-chunk-2593ideiaapp-stories@bas-software/react-native-calendars@npm_redstart/react-native-calendars@oleg97/storybook-react-native@metehankurucu/react-native-insta-story@mobile-public-skeleton/mobile-components@ngima/react-native-calendars@newbanker/react-native-calendarsgympal-calendarshotspot-client@quanle94/react-native-calendars@ratio.me/ratio-react-native-library@singone/react-native-calendars@pottercomuneo/react-native-calendars@storybook/react-native@thuong/react-native-calendar-picker@tactics/toddle-styleguide@bigbinary/neeto-commons-rn@blueshark-innovation/react-native-calendarsbkd-react-native-calendars@bumper/react-native-calendars@brunoccpires/fixed-stories-focalwebnc-streaks-calendar@apercky/react-native-calendarsmy-rn-calendars@smuxx/react-native-calendars@zalastax/nolb-react-native-sw@wasateam/react-native-calendarscentrumrespo-react-native-calendars@anatolyk/react-native-calendars@wellin/react-native-calendarsnative-calendars@bosheski/react-native-calendars@types/react-native-swipe-gestures@umairabbasi/react-native-calendars-custom@trucksmarter/react-native-calendarslidocommon@alexstrat/react-native-calendarsanother-react-native-simple-calendar@chatkitty/react-native-in-app-notificationprimeobjects-ui-mobile-calendarreact-native-animated-stickerreact-native-animated-stickers-chzreact-native-apex-calendarsreact-native-agenda-dagamoreact-native-calendars-prureact-native-calendars-servereact-native-calendar-expand-viewsreact-native-calendar-snreact-native-custom-calendarsreact-native-fb-storyreact-native-month-year-selectorreact-native-months-years-selectorreact-native-month-pickerreact-native-month-selectorreact-native-nj-calendarsreact-native-instastory-imagereact-native-notification-in-appreact-native-media-storiesreact-native-my-storiesreact-native-gallery-viewreact-native-calendars-aireact-native-calendars-dereck-newreact-native-calendars-dsgreact-native-calendars-fixreact-native-calendars-fix-dayreact-native-calendars-forkreact-native-calendars-jalalireact-native-calendars-mertdagistanreact-native-calendars-month-viewreact-native-calendars-monthly-viewreact-native-calendars-novareact-native-camerademoreact-native-daoreact-native-flash-message-custom-swipereact-native-in-app-notificationreact-native-in-app-notification-pinkreact-native-chapo-story-instareact-native-insta-storyreact-native-insta-story-2react-native-dka-rn-storiesreact-native-rndemoreact-native-schedule-dgreact-native-teesta-storyreact-native-solar-lunar-calendarsreact-native-stories-bynreact-native-stories-festoreact-native-stories-instagramreact-native-stories-mediareact-native-stories-media-srireact-native-stories-media_modreact-native-stories-plus
1.0.5

4 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago