1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x | import React, { PropTypes, Component } from 'react'; import { View, Animated, PanResponder, Dimensions, StyleSheet, LayoutAnimation, UIManager, } from 'react-native'; const SCREEN_WIDTH = Dimensions.get('window').width; const SWIPE_THRESHOLD = 0.4 * SCREEN_WIDTH; const MOVE_THRESHOLD = 50; export default class SwipeDeck extends Component { static defaultProps = { onSwipeRight: () => {}, onSwipeLeft: () => {}, }; constructor(props) { super(props); const position = new Animated.ValueXY(); const panResponder = PanResponder.create({ // ignore touch and handle only move-gestures onMoveShouldSetPanResponderCapture: (e, gesture) => { return Math.abs(gesture.dx) > MOVE_THRESHOLD; }, onStartShouldSetPanResponder: () => true, onPanResponderMove: (event, gesture) => { position.setValue({ x: gesture.dx, y: gesture.dy }); }, onPanResponderRelease: (event, gesture) => { if (gesture.dx > SWIPE_THRESHOLD) { this.forceSwipe('right'); } else if (gesture.dx < -SWIPE_THRESHOLD) { this.forceSwipe('left'); } else { this.resetPosition(); } }, }); this.state = { panResponder, position, index: 0 }; } componentWillReceiveProps(nextProps) { if (nextProps.data !== this.props.data) { this.setState({ index: 0 }); } } componentWillUpdate() { UIManager.setLayoutAnimationEnabledExperimental && UIManager.setLayoutAnimationEnabledExperimental(true); LayoutAnimation.spring(); } forceSwipe(direction) { const x = direction === 'right' ? SCREEN_WIDTH : -SCREEN_WIDTH; Animated.timing(this.state.position, { toValue: { x: x * 2, y: direction === 'right' ? -x : x }, duration: 750, }).start(() => this.onSwipeComplete(direction)); } onSwipeComplete(direction) { const { onSwipeRight, onSwipeLeft, data } = this.props; const item = data[this.state.index]; direction === 'right' ? onSwipeRight(item) : onSwipeLeft(item); this.state.position.setValue({ x: 0, y: 0 }); this.setState({ index: this.state.index + 1 }); } resetPosition() { Animated.spring(this.state.position, { toValue: { x: 0, y: 0 }, }).start(); } getCardStyle() { const { position } = this.state; const rotate = position.x.interpolate({ inputRange: [-SCREEN_WIDTH * 2, 0, SCREEN_WIDTH * 2], outputRange: ['-60deg', '0deg', '60deg'], }); return { ...this.state.position.getLayout(), transform: [{ rotate }], }; } renderCards() { Iif (this.state.index >= this.props.data.length) { return this.props.renderNoMoreCards(); } return this.props.data .map((item, i) => { Iif (i < this.state.index) { return null; } else if (i === this.state.index) { return ( <Animated.View key={item.id} style={[this.getCardStyle(), styles.cardStyle]} {...this.state.panResponder.panHandlers} > {this.props.renderCard(item)} </Animated.View> ); } return ( <Animated.View key={item.id} style={styles.cardStyle}> {this.props.renderCard(item)} </Animated.View> ); }) .reverse(); } render() { return ( <View> {this.renderCards()} </View> ); } } const styles = StyleSheet.create({ cardStyle: { position: 'absolute', width: SCREEN_WIDTH, }, }); SwipeDeck.propTypes = { data: PropTypes.any, renderCard: PropTypes.any, renderNoMoreCards: PropTypes.any, onSwipeRight: PropTypes.any, onSwipeLeft: PropTypes.any, }; |