|
| 1 | +import * as React from 'react'; |
| 2 | +import { Dimensions, View } from 'react-native'; |
| 3 | +import Animated, { |
| 4 | + interpolate, |
| 5 | + interpolateColor, |
| 6 | + useAnimatedStyle, |
| 7 | +} from 'react-native-reanimated'; |
| 8 | +import Carousel from '../../../src/index'; |
| 9 | +import type { TAnimationStyle } from '../../../src/layouts/BaseLayout'; |
| 10 | +import { SBItem } from '../components/SBItem'; |
| 11 | +import SButton from '../components/SButton'; |
| 12 | + |
| 13 | +const window = Dimensions.get('window'); |
| 14 | +const PAGE_WIDTH = window.width; |
| 15 | + |
| 16 | +function Index() { |
| 17 | + const [isAutoPlay, setIsAutoPlay] = React.useState(false); |
| 18 | + const animationStyle: TAnimationStyle = React.useCallback( |
| 19 | + (value: number) => { |
| 20 | + 'worklet'; |
| 21 | + |
| 22 | + const zIndex = interpolate(value, [-1, 0, 1], [-1000, 0, 1000]); |
| 23 | + const translateX = interpolate( |
| 24 | + value, |
| 25 | + [-1, 0, 1], |
| 26 | + [0, 0, PAGE_WIDTH] |
| 27 | + ); |
| 28 | + |
| 29 | + return { |
| 30 | + transform: [{ translateX }], |
| 31 | + zIndex, |
| 32 | + }; |
| 33 | + }, |
| 34 | + [] |
| 35 | + ); |
| 36 | + |
| 37 | + return ( |
| 38 | + <View style={{ flex: 1 }}> |
| 39 | + <Carousel |
| 40 | + loop={true} |
| 41 | + autoPlay={isAutoPlay} |
| 42 | + style={{ width: PAGE_WIDTH, height: 240 }} |
| 43 | + width={PAGE_WIDTH} |
| 44 | + data={[...new Array(6).keys()]} |
| 45 | + renderItem={({ index, animationValue }) => { |
| 46 | + return ( |
| 47 | + <CustomItem |
| 48 | + key={index} |
| 49 | + index={index} |
| 50 | + animationValue={animationValue} |
| 51 | + /> |
| 52 | + ); |
| 53 | + }} |
| 54 | + customAnimation={animationStyle} |
| 55 | + /> |
| 56 | + <SButton |
| 57 | + onPress={() => { |
| 58 | + setIsAutoPlay(!isAutoPlay); |
| 59 | + }} |
| 60 | + > |
| 61 | + AutoPlay:{`${isAutoPlay}`} |
| 62 | + </SButton> |
| 63 | + </View> |
| 64 | + ); |
| 65 | +} |
| 66 | + |
| 67 | +interface ItemProps { |
| 68 | + index: number; |
| 69 | + animationValue: Animated.SharedValue<number>; |
| 70 | +} |
| 71 | +const CustomItem: React.FC<ItemProps> = ({ index, animationValue }) => { |
| 72 | + const maskStyle = useAnimatedStyle(() => { |
| 73 | + const backgroundColor = interpolateColor( |
| 74 | + animationValue.value, |
| 75 | + [-1, 0, 1], |
| 76 | + ['#000000dd', 'transparent', '#000000dd'] |
| 77 | + ); |
| 78 | + |
| 79 | + return { |
| 80 | + backgroundColor, |
| 81 | + }; |
| 82 | + }, [animationValue]); |
| 83 | + |
| 84 | + return ( |
| 85 | + <View style={{ flex: 1 }}> |
| 86 | + <SBItem key={index} index={index} style={{ borderRadius: 0 }} /> |
| 87 | + <Animated.View |
| 88 | + pointerEvents="none" |
| 89 | + style={[ |
| 90 | + { |
| 91 | + position: 'absolute', |
| 92 | + top: 0, |
| 93 | + left: 0, |
| 94 | + right: 0, |
| 95 | + bottom: 0, |
| 96 | + }, |
| 97 | + maskStyle, |
| 98 | + ]} |
| 99 | + /> |
| 100 | + </View> |
| 101 | + ); |
| 102 | +}; |
| 103 | + |
| 104 | +export default Index; |
0 commit comments