Created
May 9, 2025 16:57
-
-
Save hungtrn75/80d40cca32e9560a0f7d63569fd14aa6 to your computer and use it in GitHub Desktop.
Shake animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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