-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseIosWidget.ts
More file actions
51 lines (43 loc) · 1.14 KB
/
useIosWidget.ts
File metadata and controls
51 lines (43 loc) · 1.14 KB
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
import {useEffect} from 'react';
import {
Easing,
Extrapolation,
interpolate,
useAnimatedReaction,
useAnimatedStyle,
useSharedValue,
withRepeat,
withTiming,
} from 'react-native-reanimated';
const {CLAMP} = Extrapolation;
const LOOP_ANIMATION = {duration: 10000, easing: Easing.linear};
const PROGRESS_INPUT = [0, 25, 50, 75, 100];
const X_OUTPUT_DEGREES = [5, 5, -5, -5, 5];
const Y_OUTPUT_DEGREES = [-5, 5, 5, -5, -5];
function useIosWidget() {
const progress = useSharedValue(0);
const x = useSharedValue(0);
const y = useSharedValue(0);
useEffect(() => {
progress.value = withRepeat(withTiming(100, LOOP_ANIMATION), Infinity);
}, []);
useAnimatedReaction(
() => progress.value,
data => {
x.value = interpolate(data, PROGRESS_INPUT, X_OUTPUT_DEGREES, CLAMP);
y.value = interpolate(data, PROGRESS_INPUT, Y_OUTPUT_DEGREES, CLAMP);
},
[progress],
);
const style = useAnimatedStyle(() => {
return {
transform: [
{perspective: 600},
{rotateX: `${x.value}deg`},
{rotateY: `${y.value}deg`},
],
};
}, [x, y]);
return {style};
}
export {useIosWidget};