universal-panresponder
A universal PanResponder API.
Last updated 8 years ago by yacheng .
BSD-3-Clause · Repository · Bugs · Original npm · Tarball · package.json
$ npm install universal-panresponder 
SYNC missed versions from official npm registry.

universal-panresponder npm

A solution to the touch gesture. Reference to the React Native PanResponder

Install

$ npm install universal-panresponder --save

Usage

import {createElement, Component, render, findDOMNode} from 'rax';
import {View} from 'rax-components';
import PanResponder from 'universal-panresponder';

const CIRCLE_SIZE = 80;
const CIRCLE_COLOR = 'blue';
const CIRCLE_HIGHLIGHT_COLOR = 'green';

class PanResponderSample extends Component {

  componentWillMount () {
    this._panResponder = PanResponder.create({
      onStartShouldSetPanResponder: this._handleStartShouldSetPanResponder,
      onMoveShouldSetPanResponder: this._handleMoveShouldSetPanResponder,
      onPanResponderGrant: this._handlePanResponderGrant,
      onPanResponderMove: this._handlePanResponderMove,
      onPanResponderRelease: this._handlePanResponderEnd,
      onPanResponderTerminate: this._handlePanResponderEnd,
    });
    this._previousLeft = 20;
    this._previousTop = 84;
    this._circleStyles = {
      style: {
        left: this._previousLeft,
        top: this._previousTop
      }
    };
  }

  componentDidMount () {
    this._updatePosition();
  }

  render () {
    return (
      <View style={styles.container}>
        <View
          ref={(circle) => { this.circle = circle; }}
          style={styles.circle}
          {...this._panResponder.panHandlers}
        />
      </View>
    );
  }

  _highlight() {
    if (this.circle && this.circle.setNativeProps) {
      this.circle.setNativeProps({
        style: {
          backgroundColor: CIRCLE_HIGHLIGHT_COLOR
        }
      });
    }
  }

  _unHighlight() {
    if (this.circle && this.circle.setNativeProps) {
      this.circle.setNativeProps({
        style: {
          backgroundColor: CIRCLE_COLOR
        }
      });
    }
  }

  _updatePosition () {
    if (this.circle && this.circle.setNativeProps) {
      this.circle.setNativeProps(this._circleStyles);
    }
  }

  _handleStartShouldSetPanResponder (e, gestureState) {
    // Should we become active when the user presses down on the circle?
    return true;
  }

  _handleMoveShouldSetPanResponder (e, gestureState) {
    // Should we become active when the user moves a touch over the circle?
    return true;
  }

  _handlePanResponderGrant = (e, gestureState) => this._highlight();


  _handlePanResponderMove = (e, gestureState) => {
    this._circleStyles.style.left = this._previousLeft + gestureState.dx;
    this._circleStyles.style.top = this._previousTop + gestureState.dy;
    this._updatePosition();
  };

  _handlePanResponderEnd = (e, gestureState) => {
    this._unHighlight();
    this._previousLeft += gestureState.dx;
    this._previousTop += gestureState.dy;
  };
}

const styles = {
  circle: {
    width: CIRCLE_SIZE,
    height: CIRCLE_SIZE,
    borderRadius: CIRCLE_SIZE / 2,
    backgroundColor: CIRCLE_COLOR,
    position: 'absolute',
    left: 0,
    top: 0,
  },
  container: {
    flex: 1,
    paddingTop: 64,
  },
};

render(<PanResponderSample />, mountNode);

For more complete documentation, please refer to React Native PanResponder.

Current Tags

  • 0.6.5                                ...           latest (8 years ago)

83 Versions

  • 0.6.5                                ...           8 years ago
  • 0.6.4                                ...           8 years ago
  • 0.6.3                                ...           9 years ago
  • 0.6.2                                ...           9 years ago
  • 0.6.1                                ...           9 years ago
  • 0.6.0                                ...           9 years ago
  • 0.5.4                                ...           9 years ago
  • 0.5.2                                ...           9 years ago
  • 0.5.1                                ...           9 years ago
  • 0.5.0                                ...           9 years ago
  • 0.4.20                                ...           9 years ago
  • 0.4.19                                ...           9 years ago
  • 0.4.18                                ...           9 years ago
  • 0.4.17                                ...           9 years ago
  • 0.4.16                                ...           9 years ago
  • 0.4.15                                ...           9 years ago
  • 0.5.0-beta                                ...           9 years ago
  • 0.4.14                                ...           9 years ago
  • 0.4.13                                ...           9 years ago
  • 0.4.12                                ...           9 years ago
  • 0.4.11                                ...           9 years ago
  • 0.4.10                                ...           9 years ago
  • 0.4.9                                ...           9 years ago
  • 0.4.8                                ...           9 years ago
  • 0.4.7                                ...           9 years ago
  • 0.4.6                                ...           9 years ago
  • 0.4.5                                ...           9 years ago
  • 0.4.4                                ...           9 years ago
  • 0.4.3                                ...           9 years ago
  • 0.4.2                                ...           9 years ago
  • 0.4.1                                ...           9 years ago
  • 0.4.0                                ...           9 years ago
  • 0.3.8                                ...           9 years ago
  • 0.3.7                                ...           9 years ago
  • 0.3.6                                ...           9 years ago
  • 0.3.5                                ...           10 years ago
  • 0.3.4                                ...           10 years ago
  • 0.3.3                                ...           10 years ago
  • 0.3.2                                ...           10 years ago
  • 0.3.1                                ...           10 years ago
  • 0.3.0                                ...           10 years ago
  • 0.2.11                                ...           10 years ago
  • 0.2.10                                ...           10 years ago
  • 0.2.9                                ...           10 years ago
  • 0.2.8                                ...           10 years ago
  • 0.2.7                                ...           10 years ago
  • 0.2.6                                ...           10 years ago
  • 0.2.5                                ...           10 years ago
  • 0.2.4                                ...           10 years ago
  • 0.2.3                                ...           10 years ago
  • 0.2.2                                ...           10 years ago
  • 0.2.1                                ...           10 years ago
  • 0.2.0                                ...           10 years ago
  • 0.1.9                                ...           10 years ago
  • 0.1.8                                ...           10 years ago
  • 0.1.7                                ...           10 years ago
  • 0.1.6                                ...           10 years ago
  • 0.1.5                                ...           10 years ago
  • 0.1.4                                ...           10 years ago
  • 0.1.3                                ...           10 years ago
  • 0.1.2                                ...           10 years ago
  • 0.1.1                                ...           10 years ago
  • 0.1.0                                ...           10 years ago
  • 0.0.19                                ...           10 years ago
  • 0.0.18                                ...           10 years ago
  • 0.0.17                                ...           10 years ago
  • 0.0.16                                ...           10 years ago
  • 0.0.15                                ...           10 years ago
  • 0.0.14                                ...           10 years ago
  • 0.0.13                                ...           10 years ago
  • 0.0.12                                ...           10 years ago
  • 0.0.11                                ...           10 years ago
  • 0.0.10                                ...           10 years ago
  • 0.0.9                                ...           10 years ago
  • 0.0.8                                ...           10 years ago
  • 0.0.7                                ...           10 years ago
  • 0.0.6                                ...           10 years ago
  • 0.0.5                                ...           10 years ago
  • 0.0.4                                ...           10 years ago
  • 0.0.3                                ...           10 years ago
  • 0.0.2                                ...           10 years ago
  • 0.0.1                                ...           10 years ago
  • 0.0.0                                ...           10 years ago
Downloads
Total 275
Today 9
This Week 77
This Month 167
Last Day 54
Last Week 58
Last Month 83
Dependencies (1)
Dev Dependencies (0)
None
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE