Skip to content

Instantly share code, notes, and snippets.

@mattThousand
Created December 23, 2020 04:22
Show Gist options
  • Save mattThousand/4f85955cb4f778061d414e0318c4148b to your computer and use it in GitHub Desktop.
Save mattThousand/4f85955cb4f778061d414e0318c4148b to your computer and use it in GitHub Desktop.
import React, {FunctionComponent, useEffect, useRef} from 'react';
import {View, StyleSheet, Pressable, Text, Animated} from 'react-native';
const SpringButton: FunctionComponent = () => {
const animatedScale = useRef(new Animated.Value(0)).current;
useEffect(() => {
animatedScale.setValue(1);
}, []);
const handleOnPress = () => {
animatedScale.setValue(0.8);
Animated.spring(animatedScale, {
toValue: 1,
bounciness: 24,
speed: 20,
useNativeDriver: true,
}).start();
};
return (
<View style={style.container}>
<Pressable onPress={handleOnPress}>
<Animated.View
style={[style.button, {transform: [{scale: animatedScale}]}]}>
<Text style={style.buttonText}>Press Me</Text>
</Animated.View>
</Pressable>
</View>
);
};
const style = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
button: {
backgroundColor: 'purple',
width: 200,
height: 80,
borderRadius: 40,
justifyContent: 'center',
alignItems: 'center',
},
buttonText: {
color: '#fff',
fontSize: 22,
},
});
export default SpringButton;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment