/
App.js
97 lines (87 loc) · 1.89 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
// @flow
import React from 'react';
import {
Animated,
Dimensions,
ScrollView,
StyleSheet,
Text,
View,
} from 'react-native';
import MessageQueue from 'MessageQueue';
MessageQueue.spy(true);
const SCREEN_WIDTH = Dimensions.get('window').width;
const xOffset = new Animated.Value(0);
const onScroll = Animated.event(
[{ nativeEvent: { contentOffset: { x: xOffset } } }],
{ useNativeDriver: true }
);
function CardView(props: { children?: ReactElement<*> }) {
return (
<Animated.ScrollView
scrollEventThrottle={16}
onScroll={onScroll}
horizontal
pagingEnabled
style={style.scrollView}>
{props.children}
</Animated.ScrollView>
)
}
function Page(props: { children?: ReactElement<*> }) {
return (
<View style={style.scrollPage}>
{props.children}
</View>
)
}
function Card(props: { text: string, index: number }) {
return (
<Animated.View style={[style.card, rotateTransform(props.index)]}>
<Text>{props.text}</Text>
</Animated.View>
);
}
function rotateTransform(index: number) {
return {
transform: [{
rotate: xOffset.interpolate({
inputRange: [(index - 1) * SCREEN_WIDTH, index * SCREEN_WIDTH, (index + 1) * SCREEN_WIDTH],
outputRange: ['30deg', '0deg', '-30deg'],
})
}]
};
}
export default function App() {
return (
<CardView>
<Page>
<Card text="Card 1" index={0}/>
</Page>
<Page>
<Card text="Card 2" index={1}/>
</Page>
<Page>
<Card text="Card 3" index={2}/>
</Page>
</CardView>
);
}
const style = StyleSheet.create({
scrollView: {
flexDirection: 'row',
},
scrollPage: {
width: SCREEN_WIDTH,
padding: 20,
},
card: {
height: 300,
justifyContent: 'center',
alignItems: 'center',
borderRadius: 4,
borderColor: 'black',
borderWidth: 1,
backgroundColor: '#F5FCFF',
}
});