Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import {
  3. Animated,
  4. ScrollView,
  5. StyleSheet,
  6. View,
  7. } from 'react-native';
  8.  
  9. const styles = StyleSheet.create({
  10. container: {
  11. flex: 1,
  12. },
  13. header: {
  14. position: 'absolute',
  15. top: 0,
  16. left: 0,
  17. right: 0,
  18. overflow: 'hidden',
  19. },
  20. headerBackground: {
  21. position: 'absolute',
  22. top: 0,
  23. left: 0,
  24. right: 0,
  25. width: null,
  26. resizeMode: 'cover',
  27. },
  28. headerChildren: {
  29. backgroundColor: 'transparent',
  30. justifyContent: 'center',
  31. },
  32. blackOverlay: {
  33. backgroundColor: 'black',
  34. position: 'absolute',
  35. top: 0,
  36. right: 0,
  37. left: 0,
  38. bottom: 0,
  39. zIndex: 100,
  40. },
  41. });
  42.  
  43.  
  44. class ImageHeaderScrollView extends Component {
  45. constructor(props) {
  46. super(props);
  47. this.state = {
  48. scrollY: new Animated.Value(0),
  49. };
  50. }
  51.  
  52. interpolateOnImageHeight(outputRange) {
  53. const headerScrollDistance = this.props.maxHeight - this.props.minHeight;
  54. return this.state.scrollY.interpolate({
  55. inputRange: [0, headerScrollDistance],
  56. outputRange,
  57. extrapolate: 'clamp',
  58. });
  59. }
  60.  
  61. renderHeader() {
  62. const headerHeight = this.interpolateOnImageHeight([this.props.maxHeight, this.props.minHeight]);
  63. const overlayOpacity = this.interpolateOnImageHeight([0, this.props.maxOverlayOpacity]);
  64.  
  65. const headerScale = this.state.scrollY.interpolate({
  66. inputRange: [-this.props.maxHeight, 0],
  67. outputRange: [3, 1],
  68. extrapolate: 'clamp',
  69. });
  70.  
  71. return (
  72. <Animated.View style={[styles.header, { height: headerHeight, transform: [{ scale: headerScale }] }]}>
  73. <Animated.View style={[styles.blackOverlay, { opacity: overlayOpacity }]} />
  74. { this.props.renderHeader() }
  75. </Animated.View>
  76. );
  77. }
  78.  
  79. render() {
  80. return (
  81. <View style={styles.container}>
  82. <ScrollView
  83. style={styles.container}
  84. scrollEventThrottle={16}
  85. onScroll={Animated.event(
  86. [{ nativeEvent: { contentOffset: { y: this.state.scrollY } } }],
  87. )}
  88. >
  89. <Animated.View style={[{ paddingTop: this.props.maxHeight }, this.props.childrenStyle]}>
  90. {this.props.children}
  91. </Animated.View>
  92. </ScrollView>
  93. { this.renderHeader() }
  94. </View>
  95. );
  96. }
  97. }
  98.  
  99. ImageHeaderScrollView.propTypes = {
  100. renderHeader: React.PropTypes.func,
  101. maxHeight: React.PropTypes.number,
  102. minHeight: React.PropTypes.number,
  103. children: React.PropTypes.node || React.PropTypes.nodes,
  104. maxOverlayOpacity: React.PropTypes.number,
  105. childrenStyle: View.propTypes.style,
  106. };
  107.  
  108. ImageHeaderScrollView.defaultProps = {
  109. maxHeight: 125,
  110. minHeight: 80,
  111. maxOverlayOpacity: 0.3,
  112. renderHeader: () => <View />,
  113. };
  114.  
  115. export default ImageHeaderScrollView;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement