Skip to content

Instantly share code, notes, and snippets.

@zidniryi
Created October 6, 2023 07:07
Show Gist options
  • Save zidniryi/d7fb70bb3cf72d73a23ca8c65623e6f6 to your computer and use it in GitHub Desktop.
Save zidniryi/d7fb70bb3cf72d73a23ca8c65623e6f6 to your computer and use it in GitHub Desktop.
import React, { useState } from 'react';
import { View, Text, StyleSheet, TouchableOpacity, Animated } from 'react-native';
const App = () => {
const [slideAnim] = useState(new Animated.Value(0));
const [isSlideIn, setIsSlideIn] = useState(false);
const slideIn = () => {
Animated.timing(slideAnim, {
toValue: 1,
duration: 500, // Adjust the duration as needed
useNativeDriver: true,
}).start();
setIsSlideIn(true);
};
const slideOut = () => {
Animated.timing(slideAnim, {
toValue: 0,
duration: 500,
useNativeDriver: true,
}).start();
setIsSlideIn(false);
};
return (
<View style={styles.container}>
<TouchableOpacity onPress={isSlideIn ? slideOut : slideIn}>
<Text style={styles.buttonText}>{isSlideIn ? 'Slide Out' : 'Slide In'}</Text>
</TouchableOpacity>
<Animated.View
style={[
styles.animatedContainer,
{
transform: [
{
translateY: slideAnim.interpolate({
inputRange: [0, 1],
outputRange: [300, 0], // Adjust the values for slide distance
}),
},
],
},
]}
>
<Text style={styles.text}>Slide me in!</Text>
</Animated.View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
animatedContainer: {
width: 200,
height: 100,
backgroundColor: 'lightblue',
justifyContent: 'center',
alignItems: 'center',
},
buttonText: {
fontSize: 18,
marginBottom: 20,
color: 'blue',
},
text: {
fontSize: 20,
color: 'white',
},
});
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment