Skip to content

Instantly share code, notes, and snippets.

@pavermakov
Created September 13, 2019 20:56
Show Gist options
  • Save pavermakov/03d4a92eefe6e81317ae33324d6c64c9 to your computer and use it in GitHub Desktop.
Save pavermakov/03d4a92eefe6e81317ae33324d6c64c9 to your computer and use it in GitHub Desktop.
React native slider screen
import React from "react";
import { View, Animated, Text, StyleSheet, Button, Dimensions } from "react-native";
const DEVICE_WIDTH = Dimensions.get("window").width;
const SliderScreen = () => {
const animateRight = new Animated.Value(0);
const moveOver = () => {
Animated.timing(animateRight, {
toValue: -DEVICE_WIDTH,
duration: 300,
useNativeDriver: true
}).start();
};
const moveBack = () => {
Animated.timing(animateRight, {
toValue: 0,
duration: 300,
useNativeDriver: true
}).start();
};
return (
<Animated.View style={[s.root, { transform: [{ translateX: animateRight }] }]}>
<View style={[s.screen, s.main]}>
<Text>Main Screen</Text>
<Button
title="Slide over"
onPress={moveOver}
/>
</View>
<View style={[s.screen, s.right]}>
<Text>Right Screen</Text>
<Button
title="Slide back"
onPress={moveBack}
/>
</View>
</Animated.View>
);
};
const s = StyleSheet.create({
root: {
flex: 1,
flexDirection: "row",
width: DEVICE_WIDTH * 2,
backgroundColor: "orange"
},
screen: {
flex: 1,
justifyContent: "center",
alignItems: "center"
},
main: {
backgroundColor: "greenyellow"
},
right: {
backgroundColor: "green"
}
});
export default SliderScreen;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment