forked from jpapillon/react-native-carousel-pager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCarouselPager.js
275 lines (246 loc) · 7.88 KB
/
CarouselPager.js
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import React, { Component } from 'react';
import {
View,
PanResponder,
Animated,
StyleSheet
} from 'react-native';
import PropTypes from 'prop-types';
export default class CarouselPager extends Component {
static propTypes = {
initialPage: PropTypes.number,
vertical: PropTypes.bool,
blurredZoom: PropTypes.number,
blurredOpacity: PropTypes.number,
animationDuration: PropTypes.number,
containerPadding: PropTypes.number,
pageSpacing: PropTypes.number,
pageStyle: PropTypes.object,
onPageChange: PropTypes.func,
deltaDelay: PropTypes.number,
children: PropTypes.array.isRequired
}
static defaultProps = {
initialPage: 0,
blurredZoom: 0.8,
blurredOpacity: 0.8,
animationDuration: 150,
containerPadding: 30,
pageSpacing: 10,
vertical: false,
deltaDelay: 0,
onPageChange: () => {},
}
state = {
width: 0,
height: 0
}
componentWillReceiveProps = (nextProps) => {
if (nextProps.children.length !== this.props.children.length) {
this.setState(() => ({
width: 0,
height: 0
}));
}
}
_getPosForPage(pageNb) {
return -pageNb * this._boxSizeInterval;
}
_getPageForOffset(offset, diff) {
let boxPos = Math.abs(offset / this._boxSizeInterval);
let index;
if (diff < 0) {
// Scrolling forwards
index = Math.ceil(boxPos);
} else {
// Scrolling backwards
index = Math.floor(boxPos);
}
// Make sure index is within bounds
if (index < 0) {
index = 0;
} else if (index > this.props.children.length - 1) {
index = this.props.children.length - 1;
}
return index;
}
_runAfterMeasurements(width, height) {
// Set box and box interval size
let length = this.props.vertical ? height : width;
this._boxSize = length - (2 * this.props.containerPadding);
this._boxSizeInterval = this._boxSize + this.props.pageSpacing;
// Get initial page
let initialPage = this.props.initialPage || 0;
if (initialPage < 0) {
initialPage = 0;
} else if (initialPage >= this.props.children.length) {
initialPage = this.props.children.length - 1;
}
this._currentPage = initialPage;
this._lastPos = this._getPosForPage(this._currentPage);
let viewsScale = [];
let viewsOpacity = [];
for (let i = 0; i < this.props.children.length; ++i) {
viewsScale.push(new Animated.Value(i === this._currentPage ? 1 : this.props.blurredZoom));
viewsOpacity.push(new Animated.Value(i === this._currentPage ? 1 : this.props.blurredOpacity));
}
this.setState({
width,
height,
pos: new Animated.Value(this._getPosForPage(this._currentPage)),
viewsScale,
viewsOpacity
});
}
animateToPage(page) {
let animations = [];
if (this._currentPage !== page) {
// New page needs to be shown (adjust opacity and scale)
animations.push(
Animated.timing(this.state.viewsScale[page], {
toValue: 1,
duration: this.props.animationDuration
})
);
animations.push(
Animated.timing(this.state.viewsOpacity[page], {
toValue: 1,
duration: this.props.animationDuration
})
);
animations.push(
Animated.timing(this.state.viewsScale[this._currentPage], {
toValue: this.props.blurredZoom,
duration: this.props.animationDuration
})
);
animations.push(
Animated.timing(this.state.viewsOpacity[this._currentPage], {
toValue: this.props.blurredOpacity,
duration: this.props.animationDuration
})
);
}
// Move to proper position for selected page
let toValue = this._getPosForPage(page);
animations.push(
Animated.timing(this.state.pos, {
toValue: toValue,
duration: this.props.animationDuration
})
);
Animated.parallel(animations).start();
this._lastPos = toValue;
this._currentPage = page;
this.props.onPageChange(page);
}
goToPage(index) {
if (index < 0 || index > this.props.children.length - 1) {
// Out of bounds, don't go anywhere
return;
}
this.animateToPage(index);
}
componentWillMount() {
this._panResponder = PanResponder.create({
onStartShouldSetPanResponder: (evt, gestureState) => {
const dx = Math.abs(gestureState.dx);
const dy = Math.abs(gestureState.dy);
return this.props.vertical ? (dy > this.props.deltaDelay && dy > dx) : (dx > this.props.deltaDelay && dx > dy);
},
onStartShouldSetPanResponderCapture: (evt, gestureState) => false,
onMoveShouldSetPanResponder: (evt, gestureState) => {
// Set PanResponder only if it is a gesture in the right direction
const dx = Math.abs(gestureState.dx);
const dy = Math.abs(gestureState.dy);
return this.props.vertical ? (dy > this.props.deltaDelay && dy > dx) : (dx > this.props.deltaDelay && dx > dy);
},
onMoveShouldSetPanResponderCapture: (evt, gestureState) => false,
onPanResponderGrant: (evt, gestureState) => {
},
onPanResponderMove: (evt, gestureState) => {
let suffix = this.props.vertical ? 'y' : 'x';
this.state.pos.setValue(this._lastPos + gestureState['d' + suffix]);
},
onPanResponderTerminationRequest: (evt, gestureState) => true,
onPanResponderRelease: (evt, gestureState) => {
let suffix = this.props.vertical ? 'y' : 'x';
this._lastPos += gestureState['d' + suffix];
let page = this._getPageForOffset(this._lastPos, gestureState['d' + suffix]);
this.animateToPage(page);
},
onPanResponderTerminate: (evt, gestureState) => {
},
onShouldBlockNativeResponder: (evt, gestureState) => true
});
}
render() {
if (!this.state.width && !this.state.height) {
// Use a transparent screen to render so we can calculate width & height
return (
<View style={{ flex: 1 }}>
<View
style={{ position: 'absolute', top: 0, left: 0, right: 0, bottom: 0, backgroundColor: 'transparent' }}
onLayout={evt => {
let width = evt.nativeEvent.layout.width;
let height = evt.nativeEvent.layout.height;
this._runAfterMeasurements(width, height);
}}
/>
</View>
);
}
let containerStyle = {};
let boxStyle = {};
if (this.props.vertical) {
containerStyle = {
top: this.state.pos,
paddingTop: this.props.containerPadding,
paddingBottom: this.props.containerPadding,
flexDirection: 'column'
}
boxStyle = {
height: this._boxSize,
marginBottom: this.props.pageSpacing
}
} else {
containerStyle = {
left: this.state.pos,
paddingLeft: this.props.containerPadding,
paddingRight: this.props.containerPadding,
flexDirection: 'row'
}
boxStyle = {
width: this._boxSize,
marginRight: this.props.pageSpacing
};
}
return (
<View style={{ flex: 1, flexDirection: this.props.vertical ? 'column' : 'row', overflow: 'hidden' }}>
<Animated.View
style={[{ flex: 1 }, containerStyle]}
{...this._panResponder.panHandlers}
>
{this.props.children.map((page, index) => {
return (
<Animated.View
key={index}
style={[{
opacity: this.state.viewsOpacity[index],
transform: [
this.props.vertical ? {
scaleX: this.state.viewsScale[index]
} : {
scaleY: this.state.viewsScale[index]
}
]
}, boxStyle, this.props.pageStyle]}>
{page}
</Animated.View>
);
})}
</Animated.View>
</View>
);
}
}