Skip to content

Commit 25d5517

Browse files
author
Andrzej Antoni Kwaśniewski
committed
simplify animated example
1 parent c506a3f commit 25d5517

1 file changed

Lines changed: 43 additions & 38 deletions

File tree

  • apps/common-app/src/new_api/showcase/animated

apps/common-app/src/new_api/showcase/animated/index.tsx

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,12 @@ export default function MinimalCard() {
1212
const rotation = useRef(new Animated.Value(0)).current;
1313
const colorProgress = useRef(new Animated.Value(0)).current;
1414
const normalisedRotation = useRef(new Animated.Value(1)).current;
15+
const opacity = useRef(new Animated.Value(1)).current;
16+
1517
let offsetX = 0;
1618
let offsetY = 0;
1719

1820
const panGesture = usePanGesture({
19-
onBegin: () => {
20-
normalisedRotation.setValue(1);
21-
Animated.timing(colorProgress, {
22-
toValue: 1,
23-
duration: 200,
24-
useNativeDriver: true,
25-
}).start();
26-
},
2721
onActivate: () => {
2822
Animated.spring(scale, {
2923
toValue: 1.05,
@@ -33,10 +27,8 @@ export default function MinimalCard() {
3327
onUpdate: (e) => {
3428
translateX.setValue(e.translationX + offsetX);
3529
translateY.setValue(e.translationY + offsetY);
36-
3730
const rotationValue = ((e.translationX + offsetX) / width) * 20;
3831
rotation.setValue(rotationValue);
39-
4032
normalisedRotation.setValue(
4133
rotationValue < 0 ? rotationValue - 1 : rotationValue + 1
4234
);
@@ -50,14 +42,37 @@ export default function MinimalCard() {
5042
if (Math.abs(e.translationX + offsetX) > width * 0.3) {
5143
const direction = e.translationX + offsetX > 0 ? 1 : -1;
5244

53-
Animated.timing(translateX, {
54-
toValue: direction * width,
55-
duration: 200,
56-
useNativeDriver: true,
57-
}).start(() => {
45+
Animated.parallel([
46+
Animated.timing(translateX, {
47+
toValue: direction * width * 1.5,
48+
duration: 300,
49+
useNativeDriver: true,
50+
}),
51+
Animated.timing(opacity, {
52+
toValue: 0,
53+
duration: 300,
54+
useNativeDriver: true,
55+
}),
56+
]).start(() => {
5857
translateX.setValue(0);
5958
translateY.setValue(0);
6059
rotation.setValue(0);
60+
normalisedRotation.setValue(1);
61+
62+
Animated.parallel([
63+
Animated.spring(scale, {
64+
toValue: 1,
65+
friction: 8,
66+
tension: 40,
67+
useNativeDriver: true,
68+
}),
69+
Animated.timing(opacity, {
70+
toValue: 1,
71+
duration: 300,
72+
useNativeDriver: true,
73+
}),
74+
]).start();
75+
6176
offsetX = 0;
6277
offsetY = 0;
6378
});
@@ -66,29 +81,26 @@ export default function MinimalCard() {
6681
toValue: 0,
6782
useNativeDriver: true,
6883
}).start();
69-
7084
Animated.spring(translateY, {
7185
toValue: 0,
7286
useNativeDriver: true,
7387
}).start();
74-
7588
Animated.spring(rotation, {
7689
toValue: 0,
7790
useNativeDriver: true,
7891
}).start();
79-
80-
const normalisedRotationValue = e.absoluteX < 0 ? -1 : 1;
81-
82-
Animated.timing(normalisedRotation, {
83-
toValue: normalisedRotationValue,
84-
duration: 500,
85-
useNativeDriver: true,
86-
}).start();
8792
}
93+
8894
offsetX = 0;
8995
offsetY = 0;
9096
},
91-
onFinalize: () => {
97+
onFinalize: (e) => {
98+
const normalisedRotationValue = e.absoluteX < 0 ? -1 : 1;
99+
Animated.timing(normalisedRotation, {
100+
toValue: normalisedRotationValue,
101+
duration: 500,
102+
useNativeDriver: true,
103+
}).start();
92104
Animated.timing(colorProgress, {
93105
toValue: 0,
94106
duration: 500,
@@ -102,18 +114,10 @@ export default function MinimalCard() {
102114
inputRange: [-20, 20],
103115
outputRange: ['-20deg', '20deg'],
104116
});
105-
const finalBackgroundColor = Animated.multiply(
106-
colorProgress,
107-
normalisedRotation
108-
).interpolate({
109-
inputRange: [-10, -1, 0, 1, 10],
110-
outputRange: [
111-
COLORS.RED,
112-
COLORS.PURPLE,
113-
COLORS.NAVY,
114-
COLORS.PURPLE,
115-
COLORS.GREEN,
116-
],
117+
118+
const finalBackgroundColor = normalisedRotation.interpolate({
119+
inputRange: [-10, 0, 10],
120+
outputRange: [COLORS.RED, COLORS.NAVY, COLORS.GREEN],
117121
extrapolate: 'clamp',
118122
});
119123

@@ -125,6 +129,7 @@ export default function MinimalCard() {
125129
styles.card,
126130
{
127131
backgroundColor: finalBackgroundColor,
132+
opacity,
128133
transform: [
129134
{ translateX },
130135
{ translateY },

0 commit comments

Comments
 (0)