-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathInfiniteCarousel.js
216 lines (191 loc) · 6.27 KB
/
InfiniteCarousel.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
import React, { Component } from 'react';
import {
View,
Text,
Animated,
Easing,
StyleSheet,
PanResponder,
} from 'react-native';
const noop = () => {};
const PAGE_RATIO = 1 / 2;
const DRAG_SENSITIVITY = 3 / 4;
const ANIMATION_DURATION = 100;
class InfiniteCarousel extends Component {
state = {
containerDimensions: {
width: 0,
height: 0,
},
contentDimensions: {
width: 0,
height: 0,
},
};
_animatedXPosition = new Animated.Value(0);
_xScrollOffset = 0;
_xScrollPosition = 0;
_pageWidth = 0;
_currentPage = 0;
_firstPosition = false;
_isDistanceEnough = velocity =>
(evt, gestureState) => Math.abs(gestureState.dx) > velocity;
_panResponder = PanResponder.create({
// Ask to be the responder:
onStartShouldSetPanResponder: (evt, gestureState) => true,
onStartShouldSetPanResponderCature: (evt, gestureState) => true,
onMoveShouldSetPanResponder: (evt, gestureState) => false,
onMoveShouldSetPanResponderCapture: this._isDistanceEnough(15),
onPanResponderGrant: (evt, gestureState) => {
// The guesture has started. Show visual feedback so the user knows
// what is happening!
// gestureState.d{x,y} will be set to zero now
this._xScrollOffset = this._xScrollPosition;
},
onPanResponderMove: (evt, gestureState) => {
// The most recent move distance is gestureState.move{X,Y}
// The accumulated gesture distance since becoming responder is
// gestureState.d{x,y}
this._scrollTo(
this._xScrollOffset + Math.round(gestureState.dx * DRAG_SENSITIVITY),
);
},
onPanResponderTerminationRequest: (evt, gestureState) => true,
onPanResponderRelease: (evt, gestureState) => {
// The user has released all touches while this view is the
// responder. This typically means a gesture has succeeded
//console.log(gestureState.vx)
this._roundPage();
},
onPanResponderTerminate: (evt, gestureState) => {
// Another component has become the responder, so this gesture
// should be cancelled
},
onShouldBlockNativeResponder: (evt, gestureState) => {
// Returns whether this component should block native components from becoming the JS
// responder. Returns true by default. Is currently only supported on android.
return true;
},
});
_scrollTo = (pos, animated, callback = noop) => {
const endOfContent = (this.state.contentDimensions.width -
this.state.containerDimensions.width) *
-1;
if (pos > 0) {
this._xScrollPosition = 0;
} else if (pos < endOfContent) {
this._xScrollPosition = endOfContent;
} else {
this._xScrollPosition = pos;
}
if (animated) {
Animated.timing(this._animatedXPosition, {
toValue: this._xScrollPosition,
duration: ANIMATION_DURATION,
easing: Easing.ease,
useNativeDriver: true,
}).start(callback);
} else {
this._animatedXPosition.setValue(this._xScrollPosition);
}
};
_getPositionToPageCenter = pageIndex =>
Math.round((pageIndex - 1) * this._pageWidth + this._pageWidth / 2) * -1;
_moveToPage = (page, animated, callback) => {
this._currentPage = page;
const pagePosition = this._getPositionToPageCenter(this._currentPage);
this._scrollTo(pagePosition, animated, callback);
};
_roundPage = () => {
const pageCenter = Math.round(this.state.containerDimensions.width / 2);
const relativePos = (this._xScrollPosition + pageCenter) / this._pageWidth;
const page = Math.abs(Math.floor(relativePos) - 1);
this._moveToPage(page, true, this._setCritical);
};
_setCritical = () => {
const pageCount = this.props.children.length;
if (this._currentPage === 1) {
this._moveToPage(pageCount + 1);
}
if (this._currentPage === pageCount + 2) {
this._moveToPage(2);
}
};
_isSameMeasure = (measurement1, measurement2) =>
measurement1.width === measurement2.width &&
measurement1.height === measurement2.height;
_manageLayout = key =>
({ nativeEvent }) => {
const dimensions = nativeEvent.layout;
if (!this._isSameMeasure(this.state[key], dimensions)) {
this.setState({ [key]: dimensions });
}
};
_renderContent() {
const { containerDimensions } = this.state;
const { children } = this.props;
this._pageWidth = containerDimensions.width * PAGE_RATIO;
// shallow copy of children
const pages = [...children];
// we want a carousel like 3-4-1-2-3-4-1-2
// push the first two children again to the end and
// unshift the last two children at the beginning
pages.push(pages[0], pages[1]);
pages.unshift(pages[children.length - 2], pages[children.length - 1]);
const compotedPageStyle = {
width: this._pageWidth,
height: containerDimensions.height,
};
const self = this;
const content = pages.map((page, index) => {
const pageOffset = (index * self._pageWidth - self._pageWidth / 2) * -1;
return (
<View key={index} style={compotedPageStyle}>
{page(self._animatedXPosition, self._pageWidth, pageOffset)}
</View>
);
});
return content;
}
componentDidUpdate() {
const { containerDimensions, contentDimensions } = this.state;
if (containerDimensions.width !== 0 && contentDimensions.width !== 0) {
if (!this._firstPosition) {
this._moveToPage(2);
this._firstPosition = true;
} else {
this._moveToPage(this._currentPage);
}
}
}
render() {
const computedContentStyle = {
transform: [{ translateX: this._animatedXPosition }],
};
return (
<View
style={styles.horizontalContainer}
onLayout={this._manageLayout('containerDimensions')}
{...this._panResponder.panHandlers}>
<View onLayout={this._manageLayout('contentDimensions')}>
<Animated.View
style={[computedContentStyle, styles.horizontalContent]}>
{this._renderContent()}
</Animated.View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
horizontalContainer: {
flexGrow: 1,
flexShrink: 1,
flexDirection: 'row',
overflow: 'scroll',
},
horizontalContent: {
flexDirection: 'row',
},
});
export default InfiniteCarousel;