Skip to content

Instantly share code, notes, and snippets.

@shubh007-dev
Last active July 4, 2019 10:40
Show Gist options
  • Save shubh007-dev/f86814b5efe379f2529c37596139733b to your computer and use it in GitHub Desktop.
Save shubh007-dev/f86814b5efe379f2529c37596139733b to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import { Platform, StyleSheet, Text, View, Dimensions, PanResponder, Animated, ScrollView } from 'react-native';
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
android:
'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
var scrollPos = null;
var that;
export default class App extends Component {
constructor(props) {
super(props);
that = this;
const {height, width} = Dimensions.get('window');
const initialPosition = {x: 0, y: height - 270}
const position = new Animated.ValueXY(initialPosition);
const parentResponder = PanResponder.create({
onMoveShouldSetPanResponderCapture: (e, gestureState) => {
return false
},
onStartShouldSetPanResponder: () => false,
onMoveShouldSetPanResponder: (e, gestureState) => {
if (this.state.toTop) {
return gestureState.dy > 6
} else {
return gestureState.dy < -6
}
},
onPanResponderTerminationRequest: () => false,
onPanResponderMove: (evt, gestureState) => {
let newy = gestureState.dy
if (this.state.toTop && newy < 0 ) return
if (this.state.toTop) {
if(scrollPos < 10){
position.setValue({x: 0, y: newy+120});
}
if(scrollPos < 10){
this.setState({scrollEnabled: false});
}
} else {
if (gestureState.dy < -50){
this.setState({opacityColor: 1});
}
position.setValue({x: 0, y: initialPosition.y + newy});
}
},
onPanResponderRelease: (evt, gestureState) => {
if (this.state.toTop) {
if (gestureState.dy > 50 && scrollPos == 0) {
this.setState({opacityColor: 0.3});
this.snapToBottom(initialPosition)
} else {
this.snapToTop()
}
} else {
if (gestureState.dy < -90) {
this.snapToTop()
} else {
this.snapToBottom(initialPosition)
}
}
},
});
this.offset = 0;
this.parentResponder = parentResponder;
this.state = { position, toTop: false, opacityColor: 0.3, scrollEnabled: false };
}
snapToTop = () => {
Animated.timing(this.state.position, {
toValue: {x: 0, y: 120},
duration: 300,
}).start(() => {});
this.setState({ toTop: true, scrollEnabled: true })
}
snapToBottom = (initialPosition) => {
Animated.timing(this.state.position, {
toValue: initialPosition,
duration: 150,
}).start(() => {});
this.setState({ toTop: false, scrollEnabled: false })
}
handleScroll(event: Object) {
scrollPos = event.nativeEvent.contentOffset.y;
}
render() {
const {height} = Dimensions.get('window');
return (
<View style={styles.container}>
<Animated.View style={{ flex: 1, opacity: this.state.opacityColor, backgroundColor: 'black' }}>
<Text style={styles.welcome}>Welcome to React Native!</Text>
<Text style={styles.instructions}>To get started, edit App.js</Text>
<Text style={styles.instructions}>{instructions}</Text>
</Animated.View>
<Animated.View style={[styles.draggable, { height }, this.state.position.getLayout()]} {...this.parentResponder.panHandlers}>
<Text style={styles.dragHandle}>=</Text>
<ScrollView
bounces={false}
scrollEventThrottle={16}
scrollEnabled={this.state.scrollEnabled}
style={styles.scroll}
onScroll={this.handleScroll}
>
<Text style={{fontSize:44}}>Lorem Ipsum</Text>
<Text style={{fontSize:44}}>dolor sit amet</Text>
<Text style={{fontSize:44}}>consectetur adipiscing elit.</Text>
<Text style={{fontSize:44}}>In ut ullamcorper leo.</Text>
<Text style={{fontSize:44}}>Sed sed hendrerit nulla,</Text>
<Text style={{fontSize:44}}>sed ullamcorper nisi.</Text>
<Text style={{fontSize:44}}>Mauris nec eros luctus</Text>
<Text style={{fontSize:44}}>leo vulputate ullamcorper</Text>
<Text style={{fontSize:44}}>et commodo nulla.</Text>
<Text style={{fontSize:44}}>Nullam id turpis vitae</Text>
<Text style={{fontSize:44}}>risus aliquet dignissim</Text>
<Text style={{fontSize:44}}>at eget quam.</Text>
<Text style={{fontSize:44}}>Nulla facilisi.</Text>
<Text style={{fontSize:44}}>Vivamus luctus lacus</Text>
<Text style={{fontSize:44}}>eu efficitur mattis</Text>
</ScrollView>
</Animated.View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
draggable: {
position: 'absolute',
right: 0,
backgroundColor: 'skyblue',
alignItems: 'center'
},
dragHandle: {
fontSize: 22,
color: '#707070',
height: 60
},
scroll: {
paddingLeft: 10,
paddingRight: 10,
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment