Skip to content

Commit

Permalink
fix: _
Browse files Browse the repository at this point in the history
  • Loading branch information
dohooo committed Dec 27, 2021
1 parent 063f564 commit c8054f1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
6 changes: 2 additions & 4 deletions example/src/stack/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ function Index() {
width: PAGE_WIDTH,
alignSelf: 'center',
justifyContent: 'center',
borderWidth: 1,
borderColor: 'red',
}}
mode="stack"
width={100}
height={100}
width={PAGE_WIDTH / 2}
height={PAGE_WIDTH / 2}
data={CAROUSEL_ITEMS}
renderItem={(backgroundColor) => (
<View
Expand Down
5 changes: 1 addition & 4 deletions src/ScrollViewGesture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import Animated, {
useSharedValue,
withDecay,
withSpring,
withTiming,
} from 'react-native-reanimated';

type GestureContext = {
Expand Down Expand Up @@ -184,9 +183,7 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
panTranslation = panTranslation * 0.5;
}

translation.value = withTiming(ctx.panOffset + panTranslation, {
duration: 0,
});
translation.value = ctx.panOffset + panTranslation;
},
onEnd: (e) => {
const { velocityX, velocityY, translationX, translationY } = e;
Expand Down
10 changes: 5 additions & 5 deletions src/hooks/useOffsetX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ export const useOffsetX = (opts: IOpts, visibleRanges: IVisibleRanges) => {

const outputRange = [
startPos,
MAX + HALF_WIDTH - 1,
MAX + HALF_WIDTH - Number.MIN_VALUE,
MIN - HALF_WIDTH,
startPos,
MAX + HALF_WIDTH,
MIN - HALF_WIDTH + 1,
MIN - HALF_WIDTH + Number.MIN_VALUE,
startPos,
];

return interpolate(
Math.round(handlerOffsetX.value),
inputRange.map(Math.round),
outputRange.map(Math.round),
handlerOffsetX.value,
inputRange,
outputRange,
Extrapolate.CLAMP
);
}
Expand Down
44 changes: 30 additions & 14 deletions src/layouts/StackLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import React from 'react';
import { StyleSheet } from 'react-native';
import { Dimensions, StyleSheet } from 'react-native';
import Animated, {
interpolate,
runOnJS,
useAnimatedReaction,
useAnimatedStyle,
useSharedValue,
withTiming,
} from 'react-native-reanimated';
import { log } from '../utils/log';
import { useOffsetX } from '../hooks/useOffsetX';
import type { IVisibleRanges } from '../hooks/useVisibleRanges';
import { LazyView } from '../LazyView';

const window = Dimensions.get('window');
const PAGE_WIDTH = window.width;

export const StackLayout: React.FC<{
loop?: boolean;
handlerOffsetX: Animated.SharedValue<number>;
Expand All @@ -22,7 +27,6 @@ export const StackLayout: React.FC<{
vertical?: boolean;
}> = (props) => {
const {
handlerOffsetX,
index,
width,
height,
Expand All @@ -33,13 +37,20 @@ export const StackLayout: React.FC<{
vertical,
} = props;

const [shouldUpdate, setShouldUpdate] = React.useState(false);
const handlerOffsetX = useSharedValue(0);

const size = React.useMemo(
() => (vertical ? height : width),
[vertical, width, height]
useAnimatedReaction(
() => props.handlerOffsetX.value,
(value) => {
handlerOffsetX.value = withTiming(value, { duration: 0 });
},
[]
);

const [shouldUpdate, setShouldUpdate] = React.useState(false);

const size = vertical ? height : width;

const x = useOffsetX(
{
handlerOffsetX,
Expand All @@ -54,41 +65,46 @@ export const StackLayout: React.FC<{
);

const offsetXStyle = useAnimatedStyle(() => {
const startPosition = (x.value - index * width) / width;
const startPosition = (x.value - index * size) / size;
runOnJS(log)(-x.value, ',', startPosition, `【${index}】`);
return {
transform: [
{
translateX: interpolate(
startPosition,
[-(index + 1), -index, 0],
[-width, 0, 0]
[-(PAGE_WIDTH - size) * 2, 0, 0]
),
},
{
scale: interpolate(
startPosition,
[-(index + 1), -index, 0, data.length - index],
[
0.7,
0.7,
0.7 - index * 0.1,
0.7 - (data.length - index) * 0.1,
1,
1,
1 - index * 0.09,
1 - (data.length - index) * 0.09,
]
),
},
{
rotateZ: `${interpolate(
startPosition,
[-(index + 1), -index, 0],
[-45, 0, 0]
[-135, 0, 0]
)}deg`,
},
{
translateY: interpolate(
startPosition,
[-(index + 1), -index, 0, data.length - index],
[0, 0, index * 40, (data.length - index) * 40]
[
0,
0,
index * size * 0.12,
(data.length - index) * size * 0.12,
]
),
},
],
Expand Down

0 comments on commit c8054f1

Please sign in to comment.