Skip to content

Commit bbe136f

Browse files
committed
simplified tap example
1 parent 5e74fcb commit bbe136f

3 files changed

Lines changed: 22 additions & 22 deletions

File tree

packages/docs-gesture-handler/static/examples/PinchGestureBasic.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export default function App() {
9797

9898
return (
9999
<GestureHandlerRootView>
100-
<View ref={containerRef} style={[styles.container]}>
100+
<View ref={containerRef} style={styles.container}>
101101
<GestureDetector gesture={pan}>
102102
<Animated.View
103103
ref={boxRef}
@@ -135,6 +135,7 @@ const styles = StyleSheet.create({
135135
flex: 1,
136136
alignItems: 'center',
137137
justifyContent: 'center',
138+
aspectRatio: 3,
138139
},
139140
box: {
140141
aspectRatio: 1,

packages/docs-gesture-handler/static/examples/TapGestureBasic.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,24 @@ import Animated, {
1515
const COLORS = ['#b58df1', '#fa7f7c', '#ffe780', '#82cab2'];
1616

1717
export default function App() {
18-
const colorIndex = useSharedValue(1);
18+
const currentIndex = useSharedValue(0);
19+
const nextIndex = useSharedValue(0);
20+
const progress = useSharedValue(0);
1921

2022
const tap = useTapGesture({
2123
onActivate: () => {
22-
if (colorIndex.value > COLORS.length) {
23-
colorIndex.value =
24-
colorIndex.value % 1 === 0 ? 1 : colorIndex.value % 1;
25-
}
26-
27-
const nextIndex = Math.ceil(colorIndex.value + 1);
28-
colorIndex.value = withTiming(nextIndex, { duration: 250 });
24+
currentIndex.value = nextIndex.value;
25+
nextIndex.value = (nextIndex.value + 1) % COLORS.length;
26+
progress.value = 0;
27+
progress.value = withTiming(1, { duration: 250 });
2928
},
3029
});
3130

3231
const animatedStyle = useAnimatedStyle(() => ({
3332
backgroundColor: interpolateColor(
34-
colorIndex.value,
35-
[0, ...COLORS.map((_, i) => i + 1), COLORS.length + 1],
36-
[COLORS[COLORS.length - 1], ...COLORS, COLORS[0]]
33+
progress.value,
34+
[0, 1],
35+
[COLORS[currentIndex.value], COLORS[nextIndex.value]]
3736
),
3837
}));
3938

packages/docs-gesture-handler/versioned_docs/version-2.x/examples/TapGestureBasic.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ import Animated, {
1515
const COLORS = ['#b58df1', '#fa7f7c', '#ffe780', '#82cab2'];
1616

1717
export default function App() {
18-
const colorIndex = useSharedValue(1);
18+
const currentIndex = useSharedValue(0);
19+
const nextIndex = useSharedValue(0);
20+
const progress = useSharedValue(0);
1921

2022
const tap = Gesture.Tap().onEnd(() => {
21-
if (colorIndex.value > COLORS.length) {
22-
colorIndex.value = colorIndex.value % 1 === 0 ? 1 : colorIndex.value % 1;
23-
}
24-
25-
const nextIndex = Math.ceil(colorIndex.value + 1);
26-
colorIndex.value = withTiming(nextIndex, { duration: 250 });
23+
currentIndex.value = nextIndex.value;
24+
nextIndex.value = (currentIndex.value + 1) % COLORS.length;
25+
progress.value = 0;
26+
progress.value = withTiming(1, { duration: 250 });
2727
});
2828

2929
const animatedStyle = useAnimatedStyle(() => ({
3030
backgroundColor: interpolateColor(
31-
colorIndex.value,
32-
[0, ...COLORS.map((_, i) => i + 1), COLORS.length + 1],
33-
[COLORS[COLORS.length - 1], ...COLORS, COLORS[0]]
31+
progress.value,
32+
[0, 1],
33+
[COLORS[currentIndex.value], COLORS[nextIndex.value]]
3434
),
3535
}));
3636

0 commit comments

Comments
 (0)