-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
DeckSwiper.js
179 lines (148 loc) · 5.72 KB
/
DeckSwiper.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
/* @flow */
'use strict';
import React from 'react';
import clamp from 'clamp';
import {Animated, PanResponder } from 'react-native';
import NativeBaseComponent from '../Base/NativeBaseComponent';
import View from './View';
var SWIPE_THRESHOLD = 120;
export default class CardSwiper extends NativeBaseComponent {
constructor(props) {
super(props);
this.state = {
pan: new Animated.ValueXY(),
pan2: new Animated.ValueXY(),
enter: new Animated.Value(0.8),
selectedItem : this.props.dataSource[0],
selectedItem2 : this.props.dataSource[1],
card1Top: true,
card2Top: false,
fadeAnim: new Animated.Value(0.8)
}
}
getInitialStyle() {
return {
topCard: {
position: 'absolute',
top: 0,
right: 0,
left: 0
}
}
}
findNextIndexes() {
let currentIndex = this.props.dataSource.indexOf(this.state.selectedItem);
let newIdx = currentIndex + 1;
let newIdx2 = currentIndex + 2;
if(newIdx2 > this.props.dataSource.length - 1 && newIdx === this.props.dataSource.length - 1) {
return [newIdx, 0];
} else if (newIdx > this.props.dataSource.length - 1) {
return [0, 1];
} else {
return [newIdx, newIdx2];
}
}
selectNext() {
let nextIndexes = this.findNextIndexes();
setTimeout( () => {
this.setState({
selectedItem: this.props.dataSource[nextIndexes[0]]
});
setTimeout( () => {
this.setState({
selectedItem2: this.props.dataSource[nextIndexes[1]]
});
}, 350);
}, 50);
}
componentWillMount() {
this._panResponder = PanResponder.create({
onMoveShouldSetResponderCapture: () => true,
onMoveShouldSetPanResponderCapture: () => true,
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: (e, gestureState) => {
let val = Math.abs((gestureState.dx*.0013));
let opa = Math.abs((gestureState.dx*.0022));
if (val>0.2) {
val = 0.2;
}
Animated.timing(
this.state.fadeAnim,
{toValue: 0.8+val}
).start();
Animated.spring(
this.state.enter,
{ toValue: 0.8+val, friction: 7 }
).start();
Animated.event([
null, {dx: this.state.pan.x},
])(e, gestureState)
},
onPanResponderRelease: (e, {vx, vy}) => {
var velocity;
if (vx >= 0) {
velocity = clamp(vx, 4.5, 10);
} else if (vx < 0) {
velocity = clamp(vx * -1, 4.5, 10) * -1;
}
if (Math.abs(this.state.pan.x._value) > SWIPE_THRESHOLD) {
if (velocity>0) {
(this.props.onSwipeRight) ? this.props.onSwipeRight() : undefined;
this.selectNext();
} else {
(this.props.onSwipeLeft) ? this.props.onSwipeLeft() : undefined;
this.selectNext();
}
Animated.decay(this.state.pan, {
velocity: {x: velocity, y: vy},
deceleration: 0.98
}).start(this._resetState.bind(this))
} else {
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.8);
this.state.fadeAnim.setValue(0.8);
this.setState({
card1Top: !this.state.card1Top,
card2Top: !this.state.card2Top
});
}
getCardStyles() {
let { pan, pan2, enter } = this.state;
let [translateX, translateY] = [pan.x, pan.y];
// let [translateX, translateY] = [pan2.x, pan2.y];
let rotate = pan.x.interpolate({inputRange: [-700, 0, 700], outputRange: ['-10deg', '0deg', '10deg']});
let opacity = pan.x.interpolate({inputRange: [-320, 0, 320], outputRange: [0.9, 1, 0.9]})
let scale = enter;
let animatedCardStyles = {transform: [{translateX}, {translateY}, {rotate}], opacity};
let animatedCardStyles2 = {transform: [{scale}]};
return [animatedCardStyles, animatedCardStyles2]
}
render() {
return(
<View ref={c => this._root = c} style={{position: 'relative', flexDirection: 'column'}}>{(this.state.selectedItem)===undefined ? (<View />) :
(<View>
<Animated.View style={[this.getCardStyles()[1],{opacity: this.state.fadeAnim}]} {...this._panResponder.panHandlers}>
{this.props.renderItem(this.state.selectedItem2)}
</Animated.View>
<Animated.View style={[ this.getCardStyles()[0], this.getInitialStyle().topCard] } {...this._panResponder.panHandlers} >
{this.props.renderItem(this.state.selectedItem)}
</Animated.View>
</View>
)
}
</View>
);
}
}