forked from meteor-factory/react-native-tinder-swipe-cards
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SwipeCards.js
416 lines (341 loc) · 11.4 KB
/
SwipeCards.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/* Gratefully copied from https://github.com/brentvatne/react-native-animated-demo-tinder */
'use strict';
import React, {Component} from 'react';
import {
StyleSheet,
Text,
View,
Animated,
PanResponder,
Image
} from 'react-native';
import clamp from 'clamp';
import Defaults from './Defaults.js';
const SWIPE_THRESHOLD = 120;
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'transparent',
},
yup: {
borderColor: 'green',
borderWidth: 2,
position: 'absolute',
padding: 20,
bottom: 20,
borderRadius: 5,
right: 20,
},
yupText: {
fontSize: 16,
color: 'green',
},
nope: {
borderColor: 'red',
borderWidth: 2,
position: 'absolute',
bottom: 20,
padding: 20,
borderRadius: 5,
left: 20,
},
nopeText: {
fontSize: 16,
color: 'red',
}
});
//Components could be unloaded and loaded and we will loose the users currentIndex, we can persist it here.
let currentIndex = {};
let guid = 0;
export default class SwipeCards extends Component {
static propTypes = {
cards: React.PropTypes.array,
cardKey: React.PropTypes.string,
loop: React.PropTypes.bool,
stack: React.PropTypes.bool,
stackGuid: React.PropTypes.string,
stackDepth: React.PropTypes.number,
stackOffsetX: React.PropTypes.number,
stackOffsetY: React.PropTypes.number,
showYup: React.PropTypes.bool,
showNope: React.PropTypes.bool,
handleYup: React.PropTypes.func,
onClickHandler: React.PropTypes.func,
handleNope: React.PropTypes.func,
renderCard: React.PropTypes.func,
renderNoMoreCards: React.PropTypes.func,
cardRemoved: React.PropTypes.func,
yupText: React.PropTypes.string,
noText: React.PropTypes.string
};
static defaultProps = {
cards: [],
cardKey: 'key',
loop: false,
stack: false,
stackDepth: 5,
stackOffsetX: 25,
stackOffsetY: 0,
showYup: true,
showNope: true,
nopeText: "Nope!",
yupText: "Yup!",
handleNope: (card) => null,
onClickHandler: () => {alert('tap')},
handleYup: (card) => null,
cardRemoved: (ix) => null,
renderCard: (card) => null,
style: styles.container
};
constructor(props) {
super(props);
//Use a persistent variable to track currentIndex instead of a local one.
this.guid = this.props.guid || guid++;
if (!currentIndex[this.guid]) currentIndex[this.guid] = 0;
this.state = {
pan: new Animated.ValueXY(),
enter: new Animated.Value(0.5),
cards: [].concat(this.props.cards),
card: this.props.cards[currentIndex[this.guid]],
};
this.lastX = 0;
this.lastY = 0;
this.cardAnimation = null;
this._panResponder = PanResponder.create({
onStartShouldSetPanResponderCapture: (e, gestureState) => {
this.lastX = gestureState.moveX;
this.lastY = gestureState.moveY;
return false;
},
onMoveShouldSetPanResponderCapture: (e, gestureState) => {
return (Math.abs(this.lastX - gestureState.moveX) > 5 || Math.abs(this.lastY - gestureState.moveY) > 5);
},
onPanResponderGrant: (e, gestureState) => {
this.state.pan.setOffset({x: this.state.pan.x._value, y: this.state.pan.y._value});
this.state.pan.setValue({x: 0, y: 0});
},
onPanResponderMove: Animated.event([
null, {dx: this.state.pan.x, dy: this.state.pan.y},
]),
onPanResponderRelease: (e, {vx, vy, dx, dy}) => {
this.state.pan.flattenOffset();
let velocity;
if ((dx === 0)&&(dy === 0)) //meaning the gesture did not cover any distance
{
this.props.onClickHandler(this.state.card)
}
if (vx > 0) {
velocity = clamp(vx, 3, 5);
} else if (vx < 0) {
velocity = clamp(vx * -1, 3, 5) * -1;
}
if (Math.abs(this.state.pan.x._value) > SWIPE_THRESHOLD) {
let cancelled = false;
if (this.state.pan.x._value > 0) {
cancelled = this.props.handleYup(this.state.card);
} else {
cancelled = this.props.handleNope(this.state.card);
}
//Yup or nope was cancelled, return the card to normal.
if (cancelled) {
this._resetPan();
return;
};
this.props.cardRemoved(currentIndex[this.guid]);
this.cardAnimation = Animated.decay(this.state.pan, {
velocity: {x: velocity, y: vy},
deceleration: 0.98
});
this.cardAnimation.start( status => {
if (status.finished) this._advanceState();
else this._resetState();
this.cardAnimation = null;
}
);
} else {
this._resetPan();
}
}
});
}
_forceLeftSwipe() {
this.cardAnimation = Animated.timing(this.state.pan, {
toValue: {x: -500, y: 0},
}).start( status => {
if (status.finished) this._advanceState();
else this._resetState();
this.cardAnimation = null;
}
);
this.props.cardRemoved(currentIndex[this.guid]);
}
_forceRightSwipe() {
this.cardAnimation = Animated.timing(this.state.pan, {
toValue: {x: 500, y: 0},
}).start( status => {
if (status.finished) this._advanceState();
else this._resetState();
this.cardAnimation = null;
}
);
this.props.cardRemoved(currentIndex[this.guid]);
}
_goToNextCard() {
currentIndex[this.guid]++;
// Checks to see if last card.
// If props.loop=true, will start again from the first card.
if (currentIndex[this.guid] > this.state.cards.length - 1 && this.props.loop) {
currentIndex[this.guid] = 0;
}
this.setState({
card: this.state.cards[currentIndex[this.guid]]
});
}
componentDidMount() {
this._animateEntrance();
}
_animateEntrance() {
Animated.spring(
this.state.enter,
{toValue: 1, friction: 8}
).start();
}
componentWillReceiveProps(nextProps) {
if (nextProps.cards !== this.props.cards) {
if (this.cardAnimation) {
this.cardAnimation.stop();
this.cardAnimation = null;
}
this.setState({
cards: [].concat(nextProps.cards),
card: nextProps.cards[0]
});
}
}
_resetPan() {
Animated.spring(this.state.pan, {
toValue: {x: 0, y: 0},
friction: 4
}).start();
}
_resetState() {
this.state.pan.setValue({x: 0, y: 0});
this.state.enter.setValue(0);
this._animateEntrance();
}
_advanceState() {
this.state.pan.setValue({x: 0, y: 0});
this.state.enter.setValue(0);
this._animateEntrance();
this._goToNextCard();
}
renderNoMoreCards() {
if (this.props.renderNoMoreCards) {
return this.props.renderNoMoreCards();
}
return <Defaults.NoMoreCards />;
}
/**
* Renders the cards as a stack with props.stackDepth cards deep.
*/
renderStack() {
if (!this.state.card) {
return this.renderNoMoreCards();
}
//Get the next stack of cards to render.
let cards = this.state.cards.slice(currentIndex[this.guid], currentIndex[this.guid] + this.props.stackDepth).reverse();
return cards.map((card, i) => {
let offsetX = this.props.stackOffsetX * cards.length - i * this.props.stackOffsetX;
let lastOffsetX = offsetX + this.props.stackOffsetX;
let offsetY = this.props.stackOffsetY * cards.length - i * this.props.stackOffsetY;
let lastOffsetY = offsetY + this.props.stackOffsetY;
let opacity = 0.25 + (0.75 / cards.length) * (i + 1);
let lastOpacity = 0.25 + (0.75 / cards.length) * i ;
let scale = 0.85 + (0.15 / cards.length) * (i + 1);
let lastScale = 0.85 + (0.15 / cards.length) * i;
let style = {
position: 'absolute',
top: this.state.enter.interpolate({inputRange: [0, 1], outputRange: [lastOffsetY, offsetY]}),
left: this.state.enter.interpolate({inputRange: [0, 1], outputRange: [lastOffsetX, offsetX]}),
opacity: this.state.enter.interpolate({inputRange: [0, 1], outputRange: [lastOpacity, opacity]}),
transform: [{scale: this.state.enter.interpolate({inputRange: [0, 1], outputRange: [lastScale, scale]})}],
elevation: i * 10
};
//Is this the top card? If so animate it and hook up the pan handlers.
if (i+1 === cards.length) {
let {pan} = this.state;
let [translateX, translateY] = [pan.x, pan.y];
let rotate = pan.x.interpolate({inputRange: [-200, 0, 200], outputRange: ["-30deg", "0deg", "30deg"]});
let opacity = pan.x.interpolate({inputRange: [-200, 0, 200], outputRange: [0.5, 1, 0.5]});
let animatedCardStyles = {
... style,
transform: [
{translateX: translateX},
{translateY: translateY},
{rotate: rotate},
{scale: this.state.enter.interpolate({inputRange: [0, 1], outputRange: [lastScale, scale]})}
]
};
return <Animated.View key={card[this.props.cardKey]} style={[styles.card, animatedCardStyles]} {... this._panResponder.panHandlers}>
{this.props.renderCard(this.state.card)}
</Animated.View>;
}
return <Animated.View key={card[this.props.cardKey]} style={style}>{this.props.renderCard(card)}</Animated.View>;
});
}
renderCard() {
if (!this.state.card) {
return this.renderNoMoreCards();
}
let {pan, enter} = this.state;
let [translateX, translateY] = [pan.x, pan.y];
let rotate = pan.x.interpolate({inputRange: [-200, 0, 200], outputRange: ["-30deg", "0deg", "30deg"]});
let opacity = pan.x.interpolate({inputRange: [-200, 0, 200], outputRange: [0.5, 1, 0.5]});
let scale = enter;
let animatedCardStyles = {transform: [{translateX}, {translateY}, {rotate}, {scale}], opacity};
return <Animated.View key={"top"} style={[styles.card, animatedCardStyles]} {... this._panResponder.panHandlers}>
{this.props.renderCard(this.state.card)}
</Animated.View>;
}
renderNope() {
let {pan} = this.state;
let nopeOpacity = pan.x.interpolate({inputRange: [-150, 0], outputRange: [1, 0]});
let nopeScale = pan.x.interpolate({inputRange: [-150, 0], outputRange: [1, 0.5], extrapolate: 'clamp'});
let animatedNopeStyles = {transform: [{scale: nopeScale}], opacity: nopeOpacity};
if (this.props.renderNope) {
return this.props.renderNope(pan);
}
if (this.props.showNope) {
return <Animated.View style={[styles.nope, animatedNopeStyles]}>
<Text style={styles.nopeText}>{this.props.nopeText}</Text>
</Animated.View>;
}
return null;
}
renderYup() {
let {pan} = this.state;
let yupOpacity = pan.x.interpolate({inputRange: [0, 150], outputRange: [0, 1]});
let yupScale = pan.x.interpolate({inputRange: [0, 150], outputRange: [0.5, 1], extrapolate: 'clamp'});
let animatedYupStyles = {transform: [{scale: yupScale}], opacity: yupOpacity};
if (this.props.renderYup) {
return this.props.renderYup(pan);
}
if (this.props.showYup) {
return <Animated.View style={[styles.yup, animatedYupStyles]}>
<Text style={styles.yupText}>{this.props.yupText}</Text>
</Animated.View>;
}
return null;
}
render() {
return (
<View style={styles.container}>
{this.props.stack ? this.renderStack() : this.renderCard()}
{this.renderNope()}
{this.renderYup()}
</View>
);
}
}