Rotate/Pusle Animation in React Native
Saumay Paul
Posted on May 22, 2024
I implemented a continuous rotation and pulsing animation for an image using React Native's Animated
API. This small gist demonstrates how to create smooth, looping animations that can enhance the visual appeal of mobile applications.
Project Overview
In this project, an image is animated to rotate continuously and pulse (scale up and down) simultaneously. Hereโs a breakdown of how I achieved this:
Code Explanation
-
Setting Up the Animated Values:
-
rotateValue
: Controls the rotation of the image. -
pulseValue
: Controls the pulsing effect (scaling) of the image.
-
-
Starting the Animations:
-
Rotation Animation:
- Wrapped in
Animated.loop
to ensure it repeats indefinitely. - Uses Animated.sequence to create a smooth rotation effect.
- Animated.timing changes rotateValue from 0 to 1 over 4000 milliseconds and from 1 to 2 over another 4000 milliseconds, creating a full rotation every 8000 milliseconds.
- The Easing.linear function ensures a consistent rotation speed.
- Wrapped in
-
Rotation Animation:
-
Pulse Animation:
- Also wrapped in
Animated.loop
to repeat indefinitely. - Uses
Animated.sequence
with twoAnimated.timing
calls to scale the image up to 1.5x its size and back down to its original size over 2000 milliseconds (1000 ms for scaling up and 1000 ms for scaling down).
- Also wrapped in
-
Combining Transformations:
- Applied to an
Animated.View
containing the image. - Transformations include
translateX
,translateY
(for spiral effect),rotate
, andscale
.
- Applied to an
Code Snippet
You can view the full code on GitHub Gist: LINK
You can view the demo on Expo Snack: LINK
Conclusion
This project showcases how powerful and flexible the Animated
API in React Native can be for creating engaging user interfaces. Animations like these can significantly improve user experience by adding a layer of interactivity and visual feedback.
Feel free to check out the code, and I'd love to hear your thoughts and feedback! ๐
Posted on May 22, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.