Skip to content

Instantly share code, notes, and snippets.

@hungtrn75
Created May 9, 2025 16:57
Show Gist options
  • Save hungtrn75/80d40cca32e9560a0f7d63569fd14aa6 to your computer and use it in GitHub Desktop.
Save hungtrn75/80d40cca32e9560a0f7d63569fd14aa6 to your computer and use it in GitHub Desktop.
Shake animation
import { useCallback } from 'react';
import {
Easing,
useAnimatedStyle,
useDerivedValue,
useSharedValue,
withRepeat,
withSequence,
withSpring,
withTiming,
} from 'react-native-reanimated';
export const useAnimatedShake = () => {
const shakeTranslateX = useSharedValue(0);
const shake = useCallback(() => {
const TranslationAmount = 10;
const timingConfig = {
// cubic-bezier(.35,.7,.5,.7)
easing: Easing.bezier(0.35, 0.7, 0.5, 0.7),
duration: 80,
};
shakeTranslateX.value = withSequence(
withTiming(TranslationAmount, timingConfig),
withRepeat(withTiming(-TranslationAmount, timingConfig), 3, true),
withSpring(0, {
mass: 0.5,
})
);
}, []);
const rStyle = useAnimatedStyle(() => {
return {
transform: [{ translateX: shakeTranslateX.value }],
};
}, []);
const isShaking = useDerivedValue(() => {
return shakeTranslateX.value !== 0;
}, []);
return { shake, rStyle, isShaking };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment