-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShake.tsx
38 lines (35 loc) · 936 Bytes
/
Shake.tsx
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
import React, { useImperativeHandle } from 'react';
import Animated, {
useAnimatedStyle,
useSharedValue,
withRepeat,
withSequence,
withTiming,
} from 'react-native-reanimated';
type Props = React.PropsWithChildren;
export type ShakeRef = {
shake: () => void;
};
export const Shake = React.forwardRef<ShakeRef, Props>((props, ref) => {
const OFFSET = 4;
const TIME = 50;
const offset = useSharedValue<number>(0);
const rStyle = useAnimatedStyle(() => ({
transform: [{ translateX: offset.value }],
}));
useImperativeHandle(
ref,
() => {
return { shake: shakeIt };
},
[]
);
const shakeIt = () => {
offset.value = withSequence(
withTiming(-OFFSET, { duration: TIME / 2 }),
withRepeat(withTiming(OFFSET, { duration: TIME }), 5, true),
withTiming(0, { duration: TIME / 2 })
);
};
return <Animated.View style={rStyle}>{props.children}</Animated.View>;
});