Skip to content

Instantly share code, notes, and snippets.

@saumaypaul20
Last active May 22, 2024 19:12
Show Gist options
  • Save saumaypaul20/ce77c19b02b83ef2e3c4421f4f506738 to your computer and use it in GitHub Desktop.
Save saumaypaul20/ce77c19b02b83ef2e3c4421f4f506738 to your computer and use it in GitHub Desktop.
This React Native code animates an image with a continuous rotation loop and a pulsating effect using the `Animated` API. Snack- https://snack.expo.dev/@saumay.paul/intrigued-blue-crackers
import React, { useEffect, useRef } from 'react';
import { Animated, View, StyleSheet,Easing } from 'react-native';
const IMAGE_URI= "https://www.airstudios.com/wp-content/uploads/2024/02/Logo-Complex-3_1.svg"
const CombinedAnimation = () => {
const rotateValue = useRef(new Animated.Value(0)).current;
const pulseValue = useRef(new Animated.Value(1)).current;
useEffect(() => {
// Rotate animation
Animated.loop(
Animated.sequence([
Animated.timing(rotateValue, {
toValue: 1,
duration: 4000,
useNativeDriver: true,
easing: Easing.linear
}),
Animated.timing(rotateValue, {
toValue: 2,
duration: 4000,
useNativeDriver: true,
easing: Easing.linear
}),
])
).start();
// Pulse animation
Animated.loop(
Animated.sequence([
Animated.timing(pulseValue, {
toValue: 1.5,
duration: 5000,
useNativeDriver: true,
}),
Animated.timing(pulseValue, {
toValue: 1,
duration: 2000,
useNativeDriver: true,
}),
])
).start();
}, [rotateValue, pulseValue]);
const combinedStyle = {
transform: [
{
rotate: rotateValue.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '360deg'],
}),
},
{
scale: pulseValue,
},
],
};
return (
<View style={styles.container}>
<Animated.View style={[styles.box, combinedStyle]}>
<Animated.Image
source={{ uri: IMAGE_URI }}
style={styles.box}
resizeMode="contain"
/>
</Animated.View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
padding:5
},
box: {
width: 150,
height: 150,
},
});
export default CombinedAnimation;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment