-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
215 lines (199 loc) · 5.95 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
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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React from 'react';
import {
ScrollView,
StatusBar,
Animated,
Dimensions,
PanResponder,
} from 'react-native';
import Hero from './Hero';
const DATA = [
{
key: 'spider_man',
color: '#C51926',
title: 'Spider Man',
name: 'Peter Parker',
description:
'Peter Benjamin Parker is a high school student and a superhero with spider-like abilities, fighting crime as his alter ego Spider-Man.',
image: require('./assets/spider_man.png'),
},
{
key: 'iron_man',
color: '#E08F04',
title: 'Iron Man',
name: 'Tony Stark',
description:
'Tony Stark is a genius, billionaire, philanthropist and the former head of Stark Industries. Using his own great wealth and exceptional technical knowledge.',
image: require('./assets/iron_man.png'),
},
{
key: 'captain_america',
color: '#204CC3',
title: 'Captain America',
name: 'Steve Rogers',
description:
'Steven Grant "Steve" Rogers was a World War II veteran, and is known as the world\'s first superhero. Born within Brooklyn, New York City.',
image: require('./assets/captain_america.png'),
},
{
key: 'doctor_strange',
color: '#ab0c0c',
title: 'Doctor Strange',
name: 'Stephen Vincent',
description:
'Stephen Vincent Strange M.D., Ph.D is a powerful sorcerer and Master of the Mystic Arts.',
image: require('./assets/doctor_strange.png'),
},
{
key: 'hulk',
color: '#875094',
title: 'Hulk',
name: 'Robert Bruce Banner',
description:
'Robert Bruce Banner, M.D., Ph.D., is a renowned scientist and a founding member of the Avengers. .',
image: require('./assets/hulk.png'),
},
];
const WINDOW_WIDTH = Dimensions.get('window').width;
const VISIBLE_ITEMS = 3;
const App = () => {
const [data, setData] = React.useState(DATA);
const scrollX = React.useRef(new Animated.Value(0)).current;
const scrollXItem = React.useRef(new Animated.Value(0)).current;
const scrollXAnimated = React.useRef(new Animated.Value(0)).current;
const [currentIndex, setCurrentIndex] = React.useState(0);
const currentIndexRef = React.useRef(0);
const setCurrentIndexRef = (newCurrentIndex) => {
currentIndexRef.current = newCurrentIndex;
setCurrentIndex(newCurrentIndex);
};
const panResponder = React.useRef(
PanResponder.create({
onMoveShouldSetPanResponder: () => true,
onPanResponderMove: (e, gestureState) => {
if (gestureState.dx < 0) {
scrollX.setValue(gestureState.dx);
scrollXItem.setValue(gestureState.dx);
}
},
onPanResponderRelease: (e, gestureState) => {
if (gestureState.dx < -120) {
Animated.timing(scrollX, {
toValue: -WINDOW_WIDTH - 100,
useNativeDriver: true,
}).start(() => {
scrollX.setValue(0);
Animated.timing(scrollXItem, {
toValue: 0,
useNativeDriver: true,
}).start();
setCurrentIndexRef(currentIndexRef.current + 1);
});
} else {
Animated.timing(scrollX, {
toValue: 0,
useNativeDriver: true,
}).start();
Animated.timing(scrollXItem, {
toValue: 0,
useNativeDriver: true,
}).start();
}
},
}),
).current;
React.useEffect(() => {
if (currentIndex == data.length - VISIBLE_ITEMS) {
const newData = [
...data.slice(currentIndex, data.length),
...data.slice(0, currentIndex),
];
setData(newData);
setCurrentIndexRef(0);
}
}, [currentIndex]);
React.useEffect(() => {
Animated.spring(scrollXAnimated, {
toValue: currentIndex,
useNativeDriver: true,
}).start();
}, [currentIndex]);
return (
<>
<StatusBar hidden />
<ScrollView
horizontal={true}
scrollEnabled={false}
contentContainerStyle={{
flex: 1,
justifyContent: 'center',
}}>
{data.map((item, index) => {
if (index < currentIndexRef.current) {
return null;
}
if (index === currentIndexRef.current) {
const rotate = scrollX.interpolate({
inputRange: [-WINDOW_WIDTH / 2, 0],
outputRange: ['-10deg', '0deg'],
extrapolate: 'clamp',
});
return (
<Animated.View
key={item.key}
{...panResponder.panHandlers}
style={{
position: 'absolute',
zIndex: data.length - index,
transform: [
{
translateX: scrollX,
},
{
rotate: rotate,
},
],
}}>
<Hero {...item} scrollX={scrollXItem} />
</Animated.View>
);
} else {
const inputRange = [index - 2, index - 1, index];
const scaleX = scrollXAnimated.interpolate({
inputRange,
outputRange: [0.9, 1, 1],
});
const translateY = scrollXAnimated.interpolate({
inputRange,
outputRange: [-20, 0, 0],
});
const opacity = scrollXAnimated.interpolate({
inputRange,
outputRange: [1 - 1 / VISIBLE_ITEMS, 1, 0],
});
return (
<Animated.View
key={item.key}
style={{
position: 'absolute',
zIndex: data.length - index,
opacity,
transform: [{scaleX}, {translateY}],
}}>
<Hero {...item} />
</Animated.View>
);
}
})}
</ScrollView>
</>
);
};
export default App;