forked from dohooo/react-native-reanimated-carousel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
186 lines (168 loc) · 5.01 KB
/
index.tsx
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
import * as React from 'react';
import { View } from 'react-native';
import Carousel from 'react-native-reanimated-carousel';
import SButton from '../components/SButton';
import { ElementsText, window } from '../constants';
import Animated, {
Extrapolate,
interpolate,
useAnimatedStyle,
} from 'react-native-reanimated';
import { withAnchorPoint } from '../utils/anchor-point';
import { fruitItems } from '../utils/items';
const colors = ['#fda282', '#fdba4e', '#800015'];
const PAGE_WIDTH = window.width;
const PAGE_HEIGHT = window.width * 1.2;
function Index() {
const [isAutoPlay, setIsAutoPlay] = React.useState(false);
const baseOptions = {
vertical: false,
width: PAGE_WIDTH,
height: PAGE_HEIGHT,
} as const;
return (
<View style={{ flex: 1 }}>
<Carousel
{...baseOptions}
loop
autoPlay={isAutoPlay}
withAnimation={{
type: 'spring',
config: {
damping: 13,
},
}}
autoPlayInterval={1500}
data={colors}
renderItem={({ index, animationValue }) => (
<Card
animationValue={animationValue}
key={index}
index={index}
/>
)}
/>
<SButton
onPress={() => {
setIsAutoPlay(!isAutoPlay);
}}
>
{ElementsText.AUTOPLAY}:{`${isAutoPlay}`}
</SButton>
</View>
);
}
const Card: React.FC<{
index: number;
animationValue: Animated.SharedValue<number>;
}> = ({ index, animationValue }) => {
const WIDTH = PAGE_WIDTH / 1.5;
const HEIGHT = PAGE_HEIGHT / 1.5;
const cardStyle = useAnimatedStyle(() => {
const scale = interpolate(
animationValue.value,
[-0.1, 0, 1],
[0.95, 1, 1],
Extrapolate.CLAMP
);
const translateX = interpolate(
animationValue.value,
[-1, -0.2, 0, 1],
[0, WIDTH * 0.3, 0, 0]
);
const transform = {
transform: [
{ scale },
{ translateX },
{ perspective: 200 },
{
rotateY: `${interpolate(
animationValue.value,
[-1, 0, 0.4, 1],
[30, 0, -25, -25],
Extrapolate.CLAMP
)}deg`,
},
],
};
return {
...withAnchorPoint(
transform,
{ x: 0.5, y: 0.5 },
{ width: WIDTH, height: HEIGHT }
),
};
}, [index]);
const blockStyle = useAnimatedStyle(() => {
const translateX = interpolate(
animationValue.value,
[-1, 0, 1],
[0, 60, 60]
);
const translateY = interpolate(
animationValue.value,
[-1, 0, 1],
[0, -40, -40]
);
const rotateZ = interpolate(
animationValue.value,
[-1, 0, 1],
[0, 0, -25]
);
return {
transform: [
{ translateX },
{ translateY },
{ rotateZ: `${rotateZ}deg` },
],
};
}, [index]);
return (
<Animated.View
style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center',
}}
>
<Animated.View
style={[
{
backgroundColor: colors[index],
alignSelf: 'center',
justifyContent: 'center',
alignItems: 'center',
borderRadius: 20,
width: WIDTH,
height: HEIGHT,
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 8,
},
shadowOpacity: 0.44,
shadowRadius: 10.32,
elevation: 16,
},
cardStyle,
]}
/>
<Animated.Image
source={fruitItems[index % 3]}
style={[
{
width: WIDTH * 0.8,
borderRadius: 16,
justifyContent: 'center',
alignItems: 'center',
position: 'absolute',
zIndex: 999,
},
blockStyle,
]}
resizeMode={'contain'}
/>
</Animated.View>
);
};
export default Index;