|
| 1 | +import * as React from 'react'; |
| 2 | +import { View } from 'react-native'; |
| 3 | +import Carousel from 'react-native-reanimated-carousel'; |
| 4 | +import Animated, { |
| 5 | + Extrapolate, |
| 6 | + interpolate, |
| 7 | + useAnimatedStyle, |
| 8 | + useSharedValue, |
| 9 | +} from 'react-native-reanimated'; |
| 10 | +import SButton from '../components/SButton'; |
| 11 | +import { SBItem } from '../components/SBItem'; |
| 12 | +import { ElementsText, window } from '../constants'; |
| 13 | +import { withAnchorPoint } from '../utils/anchor-point'; |
| 14 | + |
| 15 | +const PAGE_WIDTH = window.width / 5; |
| 16 | +const colors = [ |
| 17 | + '#26292E', |
| 18 | + '#899F9C', |
| 19 | + '#B3C680', |
| 20 | + '#5C6265', |
| 21 | + '#F5D399', |
| 22 | + '#F1F1F1', |
| 23 | +]; |
| 24 | + |
| 25 | +function Index() { |
| 26 | + const [autoPlay, setAutoPlay] = React.useState(false); |
| 27 | + const progressValue = useSharedValue<number>(0); |
| 28 | + const baseOptions = { |
| 29 | + vertical: false, |
| 30 | + width: PAGE_WIDTH, |
| 31 | + height: PAGE_WIDTH * 0.6, |
| 32 | + } as const; |
| 33 | + |
| 34 | + return ( |
| 35 | + <View |
| 36 | + style={{ |
| 37 | + alignItems: 'center', |
| 38 | + }} |
| 39 | + > |
| 40 | + <View |
| 41 | + style={{ |
| 42 | + position: 'absolute', |
| 43 | + width: 50, |
| 44 | + height: 51, |
| 45 | + backgroundColor: 'white', |
| 46 | + borderRadius: 35, |
| 47 | + transform: [{ scaleX: 10 }, { scaleY: 2 }], |
| 48 | + zIndex: 2, |
| 49 | + }} |
| 50 | + /> |
| 51 | + <View |
| 52 | + style={{ |
| 53 | + position: 'absolute', |
| 54 | + width: 50, |
| 55 | + height: 51, |
| 56 | + backgroundColor: 'white', |
| 57 | + borderRadius: 35, |
| 58 | + transform: [{ scaleX: 10 }, { scaleY: 2 }], |
| 59 | + zIndex: 2, |
| 60 | + top: 145, |
| 61 | + }} |
| 62 | + /> |
| 63 | + <Carousel |
| 64 | + {...baseOptions} |
| 65 | + loop |
| 66 | + style={{ |
| 67 | + height: window.width / 2, |
| 68 | + width: window.width, |
| 69 | + justifyContent: 'center', |
| 70 | + alignItems: 'center', |
| 71 | + }} |
| 72 | + autoPlay={autoPlay} |
| 73 | + autoPlayInterval={150} |
| 74 | + scrollAnimationDuration={600} |
| 75 | + onProgressChange={(_, absoluteProgress) => |
| 76 | + (progressValue.value = absoluteProgress) |
| 77 | + } |
| 78 | + customAnimation={(value: number) => { |
| 79 | + 'worklet'; |
| 80 | + const size = PAGE_WIDTH; |
| 81 | + const scale = interpolate( |
| 82 | + value, |
| 83 | + [-2, -1, 0, 1, 2], |
| 84 | + [1.7, 1.2, 1, 1.2, 1.7], |
| 85 | + Extrapolate.CLAMP |
| 86 | + ); |
| 87 | + |
| 88 | + const translate = interpolate( |
| 89 | + value, |
| 90 | + [-2, -1, 0, 1, 2], |
| 91 | + [-size * 1.45, -size * 0.9, 0, size * 0.9, size * 1.45] |
| 92 | + ); |
| 93 | + |
| 94 | + const transform = { |
| 95 | + transform: [ |
| 96 | + { scale }, |
| 97 | + { |
| 98 | + translateX: translate, |
| 99 | + }, |
| 100 | + { perspective: 150 }, |
| 101 | + { |
| 102 | + rotateY: `${interpolate( |
| 103 | + value, |
| 104 | + [-1, 0, 1], |
| 105 | + [30, 0, -30], |
| 106 | + Extrapolate.CLAMP |
| 107 | + )}deg`, |
| 108 | + }, |
| 109 | + ], |
| 110 | + }; |
| 111 | + |
| 112 | + return { |
| 113 | + ...withAnchorPoint( |
| 114 | + transform, |
| 115 | + { x: 0.5, y: 0.5 }, |
| 116 | + { |
| 117 | + width: baseOptions.width, |
| 118 | + height: baseOptions.height, |
| 119 | + } |
| 120 | + ), |
| 121 | + }; |
| 122 | + }} |
| 123 | + modeConfig={{ |
| 124 | + parallaxScrollingScale: 0.9, |
| 125 | + parallaxScrollingOffset: 50, |
| 126 | + }} |
| 127 | + data={colors} |
| 128 | + renderItem={({ index }) => <SBItem index={index} />} |
| 129 | + /> |
| 130 | + {!!progressValue && ( |
| 131 | + <View |
| 132 | + style={{ |
| 133 | + flexDirection: 'row', |
| 134 | + justifyContent: 'space-between', |
| 135 | + width: 100, |
| 136 | + marginTop: 10, |
| 137 | + alignSelf: 'center', |
| 138 | + }} |
| 139 | + > |
| 140 | + {colors.map((backgroundColor, index) => { |
| 141 | + return ( |
| 142 | + <PaginationItem |
| 143 | + backgroundColor={backgroundColor} |
| 144 | + animValue={progressValue} |
| 145 | + index={index} |
| 146 | + key={index} |
| 147 | + length={colors.length} |
| 148 | + /> |
| 149 | + ); |
| 150 | + })} |
| 151 | + </View> |
| 152 | + )} |
| 153 | + <SButton |
| 154 | + onPress={() => setAutoPlay(!autoPlay)} |
| 155 | + >{`${ElementsText.AUTOPLAY}:${autoPlay}`}</SButton> |
| 156 | + </View> |
| 157 | + ); |
| 158 | +} |
| 159 | + |
| 160 | +const PaginationItem: React.FC<{ |
| 161 | + index: number; |
| 162 | + backgroundColor: string; |
| 163 | + length: number; |
| 164 | + animValue: Animated.SharedValue<number>; |
| 165 | + isRotate?: boolean; |
| 166 | +}> = (props) => { |
| 167 | + const { animValue, index, length, backgroundColor, isRotate } = props; |
| 168 | + const width = 10; |
| 169 | + |
| 170 | + const animStyle = useAnimatedStyle(() => { |
| 171 | + let inputRange = [index - 1, index, index + 1]; |
| 172 | + let outputRange = [-width, 0, width]; |
| 173 | + |
| 174 | + if (index === 0 && animValue?.value > length - 1) { |
| 175 | + inputRange = [length - 1, length, length + 1]; |
| 176 | + outputRange = [-width, 0, width]; |
| 177 | + } |
| 178 | + |
| 179 | + return { |
| 180 | + transform: [ |
| 181 | + { |
| 182 | + translateX: interpolate( |
| 183 | + animValue?.value, |
| 184 | + inputRange, |
| 185 | + outputRange, |
| 186 | + Extrapolate.CLAMP |
| 187 | + ), |
| 188 | + }, |
| 189 | + ], |
| 190 | + }; |
| 191 | + }, [animValue, index, length]); |
| 192 | + return ( |
| 193 | + <View |
| 194 | + style={{ |
| 195 | + backgroundColor: 'white', |
| 196 | + width, |
| 197 | + height: width, |
| 198 | + borderRadius: 50, |
| 199 | + overflow: 'hidden', |
| 200 | + transform: [ |
| 201 | + { |
| 202 | + rotateZ: isRotate ? '90deg' : '0deg', |
| 203 | + }, |
| 204 | + ], |
| 205 | + }} |
| 206 | + > |
| 207 | + <Animated.View |
| 208 | + style={[ |
| 209 | + { |
| 210 | + borderRadius: 50, |
| 211 | + backgroundColor, |
| 212 | + flex: 1, |
| 213 | + }, |
| 214 | + animStyle, |
| 215 | + ]} |
| 216 | + /> |
| 217 | + </View> |
| 218 | + ); |
| 219 | +}; |
| 220 | + |
| 221 | +export default Index; |
0 commit comments