forked from dohooo/react-native-reanimated-carousel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
148 lines (138 loc) · 4.32 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
import * as React from 'react';
import { ImageSourcePropType, View } from 'react-native';
import Animated, {
Extrapolate,
interpolate,
useAnimatedStyle,
} from 'react-native-reanimated';
import Carousel from 'react-native-reanimated-carousel';
import type { TAnimationStyle } from '../../../src/layouts/BaseLayout';
import SButton from '../components/SButton';
import { ElementsText, window } from '../constants';
import { ImageItems } from '../utils/items';
const PAGE_WIDTH = window.width;
function Index() {
const [isAutoPlay, setIsAutoPlay] = React.useState(false);
const animationStyle: TAnimationStyle = React.useCallback(
(value: number) => {
'worklet';
const zIndex = interpolate(value, [-1, 0, 1], [300, 0, -300]);
const translateX = interpolate(value, [-1, 0, 1], [0, 0, 0]);
return {
transform: [{ translateX }],
zIndex,
};
},
[]
);
return (
<View style={{ flex: 1 }}>
<Carousel
loop={true}
autoPlay={isAutoPlay}
style={{ width: PAGE_WIDTH, height: 240 }}
width={PAGE_WIDTH}
data={[...ImageItems, ...ImageItems]}
renderItem={({ index, item, animationValue }) => {
return (
<Item
key={index}
width={PAGE_WIDTH}
animationValue={animationValue}
imageSource={item}
/>
);
}}
customAnimation={animationStyle}
scrollAnimationDuration={1200}
/>
<SButton
onPress={() => {
setIsAutoPlay(!isAutoPlay);
}}
>
{ElementsText.AUTOPLAY}:{`${isAutoPlay}`}
</SButton>
</View>
);
}
interface ItemProps {
width: number;
animationValue: Animated.SharedValue<number>;
imageSource: ImageSourcePropType;
}
const Item: React.FC<ItemProps> = ({ width, imageSource, animationValue }) => {
const leftStyle = useAnimatedStyle(() => {
const left = interpolate(
animationValue.value,
[-1, 0, 1],
[-(width / 2), 0, 0],
Extrapolate.CLAMP
);
return {
left,
};
}, [animationValue, width]);
const rightStyle = useAnimatedStyle(() => {
const right = interpolate(
animationValue.value,
[-1, 0, 1],
[-(width / 2), 0, 0],
Extrapolate.CLAMP
);
return {
right,
};
}, [animationValue, width]);
return (
<View style={{ position: 'absolute', height: '100%', width }}>
<Animated.View
style={[
{
left: 0,
position: 'absolute',
width: width / 2,
height: '100%',
overflow: 'hidden',
},
leftStyle,
]}
>
<Animated.Image
source={imageSource}
style={{
width: width,
height: '100%',
left: 0,
position: 'absolute',
}}
resizeMode="cover"
/>
</Animated.View>
<Animated.View
style={[
{
right: 0,
position: 'absolute',
width: width / 2,
height: '100%',
overflow: 'hidden',
},
rightStyle,
]}
>
<Animated.Image
source={imageSource}
style={{
width: width,
height: '100%',
right: 0,
position: 'absolute',
}}
resizeMode="cover"
/>
</Animated.View>
</View>
);
};
export default Index;