This repository has been archived by the owner on Feb 19, 2022. It is now read-only.
/
App.js
140 lines (124 loc) · 3.52 KB
/
App.js
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
import React from 'react';
import { Animated, Dimensions, PanResponder, StyleSheet, Text, View } from 'react-native';
import data from './src/data';
import Card from './src/components/Card';
import Button from './src/components/Button';
const SWIPE_DISTANCE = Dimensions.get('window').width;
const SWIPE_THRESHOLD = SWIPE_DISTANCE / 3;
export default class App extends React.Component {
state = {
items: data
};
position = new Animated.Value(0);
panResponder = PanResponder.create({
onStartShouldSetPanResponder: () => true,
onMoveShouldSetPanResponder: () => true,
onPanResponderMove: Animated.event([null, { dx: this.position }]),
onPanResponderRelease: (e, { dx, vx }) => {
if (Math.abs(dx) > SWIPE_THRESHOLD) {
const direction = dx > 0 ? 1 : -1;
const velocity = Math.max(2.5, Math.abs(vx)) * direction;
Animated.decay(this.position, {
velocity,
deceleration: 0.985,
useNativeDriver: true
}).start(this.moveToNext);
} else {
Animated.spring(this.position, {
toValue: 0,
friction: 4,
useNativeDriver: true
}).start();
}
}
});
nopePressed = () => {
Animated.spring(this.position, {
friction: 11,
tension: 60,
toValue: -SWIPE_DISTANCE,
useNativeDriver: true
}).start(this.moveToNext);
};
yepPressed = () => {
Animated.spring(this.position, {
friction: 11,
tension: 60,
toValue: SWIPE_DISTANCE,
useNativeDriver: true
}).start(this.moveToNext);
};
moveToNext = ({ finished }) => {
if (!finished) {
return;
}
this.setState(prevState => {
const [first, ...rest] = prevState.items;
return {
items: [...rest, first]
};
}, this.resetPosition);
};
resetPosition = () => {
this.position.setValue(0);
};
render() {
const [item, next] = this.state.items;
const translateX = this.position;
const rotate = Animated.divide(this.position, SWIPE_DISTANCE).interpolate({
inputRange: [-1, +1],
outputRange: ['-30deg', '30deg'],
extrapolate: 'clamp'
});
const animatedStyle = {
transform: [{ translateX }, { rotate }]
};
const nextScale = Animated.divide(this.position, SWIPE_DISTANCE).interpolate({
inputRange: [-1, -0.2, 0.2, 1],
outputRange: [1, 0.75, 0.75, 1],
extrapolate: 'clamp'
});
const nextCardStyle = {
transform: [{ scale: nextScale }]
};
return (
<View style={styles.container}>
<Animated.View key={next.text} style={[styles.card, nextCardStyle]}>
<Card image={next.image} text={next.text} />
</Animated.View>
<Animated.View
key={item.text}
style={[styles.card, animatedStyle]}
{...this.panResponder.panHandlers}
>
<Card image={item.image} text={item.text} />
</Animated.View>
<View style={styles.buttonBar}>
<Button onPress={this.nopePressed} text="Nope" backgroundColor="#ff3b30" />
<Button onPress={this.yepPressed} text="Yep" backgroundColor="#4cd964" />
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'rgb(242, 245, 253)',
alignItems: 'center',
justifyContent: 'center'
},
card: {
position: 'absolute'
},
buttonBar: {
alignItems: 'stretch',
flexDirection: 'row',
position: 'absolute',
padding: 5,
bottom: 0,
left: 0,
right: 0,
height: 75
}
});