|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import React, {Component} from 'react'; |
| 4 | +import {View, PanResponder} from 'react-native'; |
| 5 | + |
| 6 | +export const swipeDirections = { |
| 7 | + SWIPE_UP: 'SWIPE_UP', |
| 8 | + SWIPE_DOWN: 'SWIPE_DOWN', |
| 9 | + SWIPE_LEFT: 'SWIPE_LEFT', |
| 10 | + SWIPE_RIGHT: 'SWIPE_RIGHT' |
| 11 | +}; |
| 12 | + |
| 13 | +const swipeConfig = { |
| 14 | + velocityThreshold: 0.5, |
| 15 | + directionalOffsetThreshold: 40 |
| 16 | +}; |
| 17 | + |
| 18 | +function isValidSwipe(velocity, velocityThreshold, directionalOffset, directionalOffsetThreshold) { |
| 19 | + return Math.abs(velocity) > velocityThreshold && Math.abs(directionalOffset) < directionalOffsetThreshold; |
| 20 | +} |
| 21 | + |
| 22 | +class GestureRecognizer extends Component { |
| 23 | + |
| 24 | + constructor(props, context) { |
| 25 | + super(props, context); |
| 26 | + this.swipeConfig = Object.assign(swipeConfig, props.config); |
| 27 | + } |
| 28 | + |
| 29 | + componentWillReceiveProps(props) { |
| 30 | + this.swipeConfig = Object.assign(swipeConfig, props.config); |
| 31 | + } |
| 32 | + |
| 33 | + componentWillMount() { |
| 34 | + const responderEnd = this._handlePanResponderEnd.bind(this); |
| 35 | + this._panResponder = PanResponder.create({ //stop JS beautify collapse |
| 36 | + onStartShouldSetPanResponder: this._handleShouldSetPanResponder, |
| 37 | + onMoveShouldSetPanResponder: this._handleShouldSetPanResponder, |
| 38 | + onPanResponderRelease: responderEnd, |
| 39 | + onPanResponderTerminate: responderEnd |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + _handleShouldSetPanResponder(evt) { |
| 44 | + return evt.nativeEvent.touches.length === 1; |
| 45 | + } |
| 46 | + |
| 47 | + _handlePanResponderEnd(evt, gestureState) { |
| 48 | + const swipeDirection = this._getSwipeDirection(gestureState); |
| 49 | + this._triggerSwipeHandlers(swipeDirection, gestureState); |
| 50 | + } |
| 51 | + |
| 52 | + _triggerSwipeHandlers(swipeDirection, gestureState) { |
| 53 | + const {onSwipe, onSwipeUp, onSwipeDown, onSwipeLeft, onSwipeRight} = this.props; |
| 54 | + const {SWIPE_LEFT, SWIPE_RIGHT, SWIPE_UP, SWIPE_DOWN} = swipeDirections; |
| 55 | + onSwipe && onSwipe(swipeDirection, gestureState); |
| 56 | + switch (swipeDirection) { |
| 57 | + case SWIPE_LEFT: |
| 58 | + onSwipeLeft && onSwipeLeft(gestureState); |
| 59 | + break; |
| 60 | + case SWIPE_RIGHT: |
| 61 | + onSwipeRight && onSwipeRight(gestureState); |
| 62 | + break; |
| 63 | + case SWIPE_UP: |
| 64 | + onSwipeUp && onSwipeUp(gestureState); |
| 65 | + break; |
| 66 | + case SWIPE_DOWN: |
| 67 | + onSwipeDown && onSwipeDown(gestureState); |
| 68 | + break; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + _getSwipeDirection(gestureState) { |
| 73 | + const {SWIPE_LEFT, SWIPE_RIGHT, SWIPE_UP, SWIPE_DOWN} = swipeDirections; |
| 74 | + const {dx, dy} = gestureState; |
| 75 | + if (this._isValidHorizontalSwipe(gestureState)) { |
| 76 | + return (dx > 0) |
| 77 | + ? SWIPE_RIGHT |
| 78 | + : SWIPE_LEFT; |
| 79 | + } else if (this._isValidVerticalSwipe(gestureState)) { |
| 80 | + return (dy > 0) |
| 81 | + ? SWIPE_DOWN |
| 82 | + : SWIPE_UP; |
| 83 | + } |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + _isValidHorizontalSwipe(gestureState) { |
| 88 | + const {vx, dy} = gestureState; |
| 89 | + const {velocityThreshold, directionalOffsetThreshold} = this.swipeConfig; |
| 90 | + return isValidSwipe(vx, velocityThreshold, dy, directionalOffsetThreshold); |
| 91 | + } |
| 92 | + |
| 93 | + _isValidVerticalSwipe(gestureState) { |
| 94 | + const {vy, dx} = gestureState; |
| 95 | + const {velocityThreshold, directionalOffsetThreshold} = this.swipeConfig; |
| 96 | + return isValidSwipe(vy, velocityThreshold, dx, directionalOffsetThreshold); |
| 97 | + } |
| 98 | + |
| 99 | + render() { |
| 100 | + return (<View {...this.props} {...this._panResponder.panHandlers}/>); |
| 101 | + } |
| 102 | +}; |
| 103 | + |
| 104 | +export default GestureRecognizer; |
0 commit comments