forked from dohooo/react-native-reanimated-carousel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
145 lines (130 loc) · 3.3 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
import * as React from "react";
import { View } from "react-native";
import Animated, {
interpolate,
interpolateColor,
runOnJS,
useAnimatedReaction,
useAnimatedStyle,
useSharedValue,
} from "react-native-reanimated";
import Carousel, { TAnimationStyle } from "react-native-reanimated-carousel";
import { Arrow, ArrowDirection } from "./Arrow";
import { window } from "../../constants";
function Index() {
const headerHeight = 100;
const PAGE_WIDTH = window.width;
const PAGE_HEIGHT = window.height - headerHeight;
const directionAnim = useSharedValue<ArrowDirection>(
ArrowDirection.IS_VERTICAL,
);
const [isVertical, setIsVertical] = React.useState(true);
const animationStyle: TAnimationStyle = React.useCallback(
(value: number) => {
"worklet";
const translateY = interpolate(
value,
[-1, 0, 1],
[-PAGE_HEIGHT, 0, 0],
);
const translateX = interpolate(
value,
[-1, 0, 1],
[-PAGE_WIDTH, 0, 0],
);
const zIndex = interpolate(value, [-1, 0, 1], [300, 0, -300]);
const scale = interpolate(value, [-1, 0, 1], [1, 1, 0.85]);
return {
transform: [
isVertical ? { translateY } : { translateX },
{ scale },
],
zIndex,
};
},
[PAGE_HEIGHT, PAGE_WIDTH, isVertical],
);
useAnimatedReaction(
() => directionAnim.value,
(direction) => {
switch (direction) {
case ArrowDirection.IS_VERTICAL:
runOnJS(setIsVertical)(true);
break;
case ArrowDirection.IS_HORIZONTAL:
runOnJS(setIsVertical)(false);
break;
}
},
[],
);
return (
<View style={{ flex: 1 }}>
<Carousel
loop
style={{
width: PAGE_WIDTH,
height: PAGE_HEIGHT,
justifyContent: "center",
alignItems: "center",
backgroundColor: "black",
}}
vertical={isVertical}
width={PAGE_WIDTH}
height={PAGE_HEIGHT}
data={[...new Array(6).keys()]}
renderItem={({ index, animationValue }) => (
<Item
key={index}
index={index}
animationValue={animationValue}
directionAnim={directionAnim}
/>
)}
customAnimation={animationStyle}
/>
</View>
);
}
const Item: React.FC<{
index: number
animationValue: Animated.SharedValue<number>
directionAnim: Animated.SharedValue<ArrowDirection>
}> = ({ animationValue, directionAnim }) => {
const maskStyle = useAnimatedStyle(() => {
const zIndex = interpolate(
animationValue.value,
[-1, 0, 1],
[300, 0, -300],
);
const backgroundColor = interpolateColor(
animationValue.value,
[-1, 0, 1],
["transparent", "transparent", "rgba(0,0,0,0.3)"],
);
return {
backgroundColor,
zIndex,
};
}, [animationValue]);
return (
<View
style={{
backgroundColor: "white",
flex: 1,
borderRadius: 20,
justifyContent: "center",
alignItems: "center",
}}
>
<Animated.View
style={[
maskStyle,
{ position: "absolute", width: "100%", height: "100%" },
]}
/>
<Arrow directionAnim={directionAnim} />
</View>
);
};
export default Index;