-
-
Notifications
You must be signed in to change notification settings - Fork 329
/
index.tsx
157 lines (142 loc) · 4.7 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
import * as React from 'react';
import { View, Pressable } from 'react-native';
import Animated, {
Extrapolate,
interpolate,
interpolateColor,
useAnimatedStyle,
useSharedValue,
withTiming,
} from 'react-native-reanimated';
import Carousel, { ICarouselInstance } from 'react-native-reanimated-carousel';
import SButton from '../components/SButton';
import { ElementsText, window } from '../constants';
import { useToggleButton } from '../hooks/useToggleButton';
const PAGE_WIDTH = 60;
const PAGE_HEIGHT = 40;
const DATA = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
function Index() {
const r = React.useRef<ICarouselInstance>(null);
const AutoPLay = useToggleButton({
defaultValue: false,
buttonTitle: ElementsText.AUTOPLAY,
});
const [loop, setLoop] = React.useState(false);
return (
<View style={{ flex: 1 }}>
<View style={{ marginVertical: 100 }}>
<Carousel
key={`${loop}`}
ref={r}
loop={loop}
style={{
width: window.width,
height: PAGE_HEIGHT,
justifyContent: 'center',
alignItems: 'center',
borderBottomWidth: 1,
borderBottomColor: '#0071fa',
}}
width={PAGE_WIDTH}
height={PAGE_HEIGHT}
data={DATA}
renderItem={({ item, animationValue }) => {
return (
<Item
animationValue={animationValue}
label={item}
onPress={() =>
r.current?.scrollTo({
count: animationValue.value,
animated: true,
})
}
/>
);
}}
autoPlay={AutoPLay.status}
/>
</View>
{AutoPLay.button}
<SButton onPress={() => setLoop(!loop)}>{`Loop: ${loop}`}</SButton>
<View
style={{
marginTop: 24,
flexDirection: 'row',
justifyContent: 'space-evenly',
}}
>
<SButton onPress={() => r.current?.prev()}>{'Prev'}</SButton>
<SButton onPress={() => r.current?.next()}>{'Next'}</SButton>
</View>
</View>
);
}
export default Index;
interface Props {
animationValue: Animated.SharedValue<number>;
label: string;
onPress?: () => void;
}
const Item: React.FC<Props> = (props) => {
const { animationValue, label, onPress } = props;
const translateY = useSharedValue(0);
const containerStyle = useAnimatedStyle(() => {
const opacity = interpolate(
animationValue.value,
[-1, 0, 1],
[0.5, 1, 0.5],
Extrapolate.CLAMP
);
return {
opacity,
};
}, [animationValue]);
const labelStyle = useAnimatedStyle(() => {
const scale = interpolate(
animationValue.value,
[-1, 0, 1],
[1, 1.25, 1],
Extrapolate.CLAMP
);
const color = interpolateColor(
animationValue.value,
[-1, 0, 1],
['#b6bbc0', '#0071fa', '#b6bbc0']
);
return {
transform: [{ scale }, { translateY: translateY.value }],
color,
};
}, [animationValue, translateY]);
const onPressIn = React.useCallback(() => {
translateY.value = withTiming(-8, { duration: 250 });
}, [translateY]);
const onPressOut = React.useCallback(() => {
translateY.value = withTiming(0, { duration: 250 });
}, [translateY]);
return (
<Pressable
onPress={onPress}
onPressIn={onPressIn}
onPressOut={onPressOut}
>
<Animated.View
style={[
{
height: '100%',
alignItems: 'center',
justifyContent: 'center',
},
containerStyle,
]}
>
<Animated.Text
style={[{ fontSize: 18, color: '#26292E' }, labelStyle]}
>
{label}
</Animated.Text>
</Animated.View>
</Pressable>
);
};