-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathcustomAnimations.ts
171 lines (160 loc) · 4.18 KB
/
customAnimations.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { Animated } from 'react-native';
import type {
AnimationFunction,
AnimationFunctionParams,
} from 'react-native-notifier';
// utility function that returns convenient values for easier animations
const getFinalAnimationStateAndTranslateY = ({
animationState,
componentHeight,
swipeTranslationY,
}: AnimationFunctionParams) => {
const finalState = Animated.add(
Animated.divide(swipeTranslationY, componentHeight),
animationState
);
const translateY = Animated.multiply(
Animated.subtract(finalState, 1),
componentHeight
);
const stateClamped = finalState.interpolate({
inputRange: [0, 1],
outputRange: [0, 1],
extrapolate: 'clamp',
});
const translateYClamped = Animated.multiply(
Animated.subtract(stateClamped, 1),
componentHeight
);
return {
finalState,
translateY,
stateClamped,
translateYClamped,
};
};
export const opacityTransformScaleAnimationFunction: AnimationFunction = (
param
) => {
const { stateClamped, translateYClamped } =
getFinalAnimationStateAndTranslateY(param);
return {
opacity: stateClamped.interpolate({
inputRange: [0, 0.5, 1],
outputRange: [0, 0, 1],
extrapolate: 'clamp',
}),
transform: [
{
translateY: translateYClamped.interpolate({
inputRange: [-1000, 0],
outputRange: [-1000, 0],
extrapolate: 'clamp',
}),
},
{
scale: stateClamped.interpolate({
// start with scale = 1, it's a trick to make correct height calculation
inputRange: [0, 0.01, 1],
outputRange: [1, 0, 1],
extrapolate: 'clamp',
}),
},
],
};
};
export const classicWithOverSwipeAnimationFunction: AnimationFunction = (
param
) => {
const { translateY } = getFinalAnimationStateAndTranslateY(param);
return {
transform: [
{
// from negative values to 0 it is dragging as usual, but dragging down is 20x slower.
translateY: translateY.interpolate({
inputRange: [-1, 0, 20],
outputRange: [-1, 0, 1],
}),
},
],
};
};
export const opacityOnlyAnimationFunction: AnimationFunction = (param) => {
const { stateClamped } = getFinalAnimationStateAndTranslateY(param);
return {
opacity: stateClamped,
};
};
export const scaleOnlyAnimationFunction: AnimationFunction = (param) => {
const { stateClamped } = getFinalAnimationStateAndTranslateY(param);
return {
// use opacity to avoid flickering when scale = 1 while state = 0
opacity: stateClamped.interpolate({
inputRange: [0.01, 0.02],
outputRange: [0, 1],
extrapolate: 'clamp',
}),
transform: [
{
scale: stateClamped.interpolate({
inputRange: [0, 0.01, 1],
outputRange: [1, 0, 1],
extrapolate: 'clamp',
}),
},
],
};
};
export const scaleAndRotationAnimationFunction: AnimationFunction = (param) => {
const { stateClamped } = getFinalAnimationStateAndTranslateY(param);
return {
// use opacity to avoid flickering when scale = 1 while state = 0
opacity: stateClamped.interpolate({
inputRange: [0.01, 0.02],
outputRange: [0, 1],
extrapolate: 'clamp',
}),
transform: [
{
scale: stateClamped.interpolate({
inputRange: [0, 0.01, 1],
outputRange: [1, 0, 1],
extrapolate: 'clamp',
}),
},
{
rotate: stateClamped.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '360deg'],
extrapolate: 'clamp',
}),
},
],
};
};
// Code from README.md example
export const translateAndScaleAnimationFunction: AnimationFunction = (
param
) => {
const { stateClamped, translateYClamped } =
getFinalAnimationStateAndTranslateY(param);
return {
opacity: stateClamped.interpolate({
inputRange: [0.01, 0.02],
outputRange: [0, 1],
extrapolate: 'clamp',
}),
transform: [
{
translateY: translateYClamped,
},
{
scale: stateClamped.interpolate({
inputRange: [0, 0.01, 1],
outputRange: [1, 0, 1],
extrapolate: 'clamp',
}),
},
],
};
};