From f49dc2fb2b5700d38621c85395a75ace3a311e92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=9D=B1=E6=BE=94?= Date: Sun, 5 Dec 2021 16:26:49 +0800 Subject: [PATCH 1/4] refactor: refactor useOffsetX --- example/src/App.tsx | 18 ++++++++++---- src/hooks/useComputedAnim.ts | 7 ++++-- src/hooks/useOffsetX.ts | 47 ++++++++++++++++++++++++++---------- 3 files changed, 52 insertions(+), 20 deletions(-) diff --git a/example/src/App.tsx b/example/src/App.tsx index f108c471..d49aed39 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -22,6 +22,7 @@ const data: ImageSourcePropType[] = [ require('../assets/carousel-0.jpg'), require('../assets/carousel-1.jpg'), require('../assets/carousel-2.jpg'), + require('../assets/carousel-1.jpg'), ]; export default function App() { @@ -37,11 +38,18 @@ export default function App() { paddingTop: 100, }} > - + - defaultIndex={1} ref={r} - width={window.width} + style={{ borderColor: 'red', borderWidth: 1 }} + width={window.width / 4} data={data} parallaxScrollingScale={0.8} renderItem={(source) => ( @@ -78,7 +86,7 @@ export default function App() { /> - + {/* onProgressChange={(_, absoluteProgress) => { progressValue.value = absoluteProgress; }} @@ -97,7 +105,7 @@ export default function App() { /> )} - /> + /> */} {!!progressValue && ( { const { handlerOffsetX, index, width, computedAnimResult, loop } = opts; const x = useDerivedValue(() => { - const { MAX, MIN, TOTAL_WIDTH, HALF_WIDTH } = computedAnimResult; + const { ITEM_LENGTH } = computedAnimResult; + const VALID_LENGTH = ITEM_LENGTH - 1; + const TOTAL_WIDTH = width * ITEM_LENGTH; + const HALF_WIDTH = 0.5 * width; + if (loop) { - const defaultPos = width * index; - const startPos = - defaultPos > MAX - ? MAX - defaultPos - : defaultPos < MIN - ? MIN - defaultPos - : defaultPos; + function getDefaultPos( + type: 'positive' | 'negative' = 'positive', + _length: number + ) { + const defaultPos = width * index; + let length = null; + if (type === 'positive') { + length = _length; + } else { + length = VALID_LENGTH - _length; + } + const boundary = length * width; + + if (defaultPos > boundary) { + return boundary - defaultPos; + } + return defaultPos; + } + + const ccc = 2; + const startPos = getDefaultPos('positive', ccc); + + const MAX = ccc * width; + const MIN = -((VALID_LENGTH - ccc) * width); const inputRange = [ -TOTAL_WIDTH, - -(MAX + HALF_WIDTH) - startPos - 1, - -(MAX + HALF_WIDTH) - startPos, + MIN - HALF_WIDTH - startPos - 1, + MIN - HALF_WIDTH - startPos, 0, MAX + HALF_WIDTH - startPos, MAX + HALF_WIDTH - startPos + 1, @@ -38,11 +59,11 @@ export const useOffsetX = (opts: IOpts) => { const outputRange = [ startPos, - 1.5 * width - 1, - -(MAX + HALF_WIDTH), + ccc * width + HALF_WIDTH - 1, + MIN - HALF_WIDTH, startPos, MAX + HALF_WIDTH, - -(1.5 * width - 1), + -((VALID_LENGTH - ccc) * width) - HALF_WIDTH + 1, startPos, ]; From 7494522d3ef168ab511548b267b22958a641dca6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=9D=B1=E6=BE=94?= Date: Sun, 5 Dec 2021 20:54:26 +0800 Subject: [PATCH 2/4] refactor: refactor useOffsetX,support to custom carousel layout components --- example/src/App.tsx | 73 ++++++++++++++++++++-------------- src/Carousel.tsx | 14 +++---- src/CarouselItem.tsx | 9 +++-- src/hooks/useComputedAnim.ts | 26 ------------ src/hooks/useOffsetX.ts | 59 +++++++++++++++------------ src/layouts/ParallaxLayout.tsx | 7 ++-- 6 files changed, 91 insertions(+), 97 deletions(-) delete mode 100644 src/hooks/useComputedAnim.ts diff --git a/example/src/App.tsx b/example/src/App.tsx index d49aed39..082820d7 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -15,6 +15,7 @@ import Animated, { useAnimatedStyle, useSharedValue, } from 'react-native-reanimated'; +import { TouchableWithoutFeedback } from 'react-native-gesture-handler'; const window = Dimensions.get('window'); @@ -22,12 +23,12 @@ const data: ImageSourcePropType[] = [ require('../assets/carousel-0.jpg'), require('../assets/carousel-1.jpg'), require('../assets/carousel-2.jpg'), - require('../assets/carousel-1.jpg'), ]; export default function App() { const progressValue = useSharedValue(0); - const r = React.useRef(null); + const defaultCarouselRef = React.useRef(null); + const parallaxCarouselRef = React.useRef(null); return ( - ref={r} - style={{ borderColor: 'red', borderWidth: 1 }} - width={window.width / 4} + ref={defaultCarouselRef} + width={window.width} data={data} parallaxScrollingScale={0.8} renderItem={(source) => ( @@ -75,24 +72,25 @@ export default function App() {