ParallaxView.js
4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
'use strict';
var React = require('react');
var ReactNative = require('react-native');
var {
Dimensions,
StyleSheet,
View,
ScrollView,
Animated,
} = ReactNative;
/**
* BlurView temporarily removed until semver stuff is set up properly
*/
//var BlurView /* = require('react-native-blur').BlurView */;
var ScrollableMixin = require('react-native-scrollable-mixin');
var screen = Dimensions.get('window');
var ScrollViewPropTypes = ScrollView.propTypes;
var ParallaxView = React.createClass({
mixins: [ScrollableMixin],
propTypes: {
...ScrollViewPropTypes,
windowHeight: React.PropTypes.number,
backgroundSource: React.PropTypes.object,
header: React.PropTypes.node,
blur: React.PropTypes.string,
contentInset: React.PropTypes.object,
},
getDefaultProps: function () {
return {
windowHeight: 300,
contentInset: {
top: screen.scale
}
};
},
getInitialState: function () {
return {
scrollY: new Animated.Value(0)
};
},
/**
* IMPORTANT: You must return the scroll responder of the underlying
* scrollable component from getScrollResponder() when using ScrollableMixin.
*/
getScrollResponder() {
return this._scrollView.getScrollResponder();
},
setNativeProps(props) {
this._scrollView.setNativeProps(props);
},
renderBackground: function () {
var { windowHeight, backgroundSource, blur } = this.props;
var { scrollY } = this.state;
if (!windowHeight || !backgroundSource) {
return null;
}
return (
<Animated.Image
style={[styles.background, {
height: windowHeight,
transform: [{
translateY: scrollY.interpolate({
inputRange: [ -windowHeight, 0, windowHeight],
outputRange: [windowHeight/2, 0, -windowHeight/3]
})
},{
scale: scrollY.interpolate({
inputRange: [ -windowHeight, 0, windowHeight],
outputRange: [2, 1, 1]
})
}]
}]}
source={backgroundSource}>
{/*
!!blur && (BlurView || (BlurView = require('react-native-blur').BlurView)) &&
<BlurView blurType={blur} style={styles.blur} />
*/}
</Animated.Image>
);
},
renderHeader: function () {
var { windowHeight, backgroundSource } = this.props;
var { scrollY } = this.state;
if (!windowHeight || !backgroundSource) {
return null;
}
return (
<Animated.View style={{
position: 'relative',
height: windowHeight,
opacity: scrollY.interpolate({
inputRange: [-windowHeight, 0, windowHeight / 1.2],
outputRange: [1, 1, 0]
}),
}}>
{this.props.header}
</Animated.View>
);
},
render: function () {
var { style, ...props } = this.props;
return (
<View style={[styles.container, style]}>
{this.renderBackground()}
<ScrollView
ref={component => { this._scrollView = component; }}
{...props}
style={styles.scrollView}
onScroll={Animated.event(
[{ nativeEvent: { contentOffset: { y: this.state.scrollY }}}]
)}
scrollEventThrottle={16}>
{this.renderHeader()}
<View style={[styles.content, props.scrollableViewStyle]}>
{this.props.children}
</View>
</ScrollView>
</View>
);
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
borderColor: 'transparent',
},
scrollView: {
backgroundColor: 'transparent',
},
background: {
position: 'absolute',
backgroundColor: '#2e2f31',
width: screen.width,
resizeMode: 'cover'
},
blur: {
position: 'absolute',
left: 0,
right: 0,
top: 0,
bottom: 0,
backgroundColor: 'transparent',
},
content: {
shadowColor: '#222',
shadowOpacity: 0.3,
shadowRadius: 2,
backgroundColor: '#fff',
flex: 1,
flexDirection: 'column'
}
});
module.exports = ParallaxView;