forked from PaulBGD/react-native-image-slider
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathImageSlider.js
189 lines (174 loc) · 5.93 KB
/
ImageSlider.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
import React, {Component} from 'react';
import {
Image,
Text,
View,
ScrollView,
StyleSheet,
Animated,
PanResponder,
TouchableHighlight,
TouchableOpacity,
Dimensions
} from 'react-native';
const reactNativePackage = require('react-native/package.json');
const splitVersion = reactNativePackage.version.split('.');
const majorVersion = +splitVersion[0];
const minorVersion = +splitVersion[1];
const styles = StyleSheet.create({
container: {
backgroundColor: '#222'
},
containerView: {
flexDirection: 'row',
},
buttons: {
height: 15,
marginTop: -15,
flex: 1,
justifyContent: 'center',
alignItems: 'center',
flexDirection: 'row'
},
button: {
margin: 3,
width: 8,
height: 8,
borderRadius: 8 / 2,
backgroundColor: '#ccc',
opacity: 0.9
},
buttonSelected: {
opacity: 1,
backgroundColor: '#fff',
}
});
export default class ImageSlider extends Component {
constructor(props) {
super(props);
this.state = {
position: 0,
height: Dimensions.get('window').width * (4 / 9),
width: Dimensions.get('window').width,
scrolling: false,
};
}
_onRef(ref) {
this._ref = ref;
if (ref && this.state.position !== this._getPosition()) {
this._move(this._getPosition());
}
}
_move(index) {
const isUpdating = index !== this._getPosition();
const x = this.state.width * index;
if (majorVersion === 0 && minorVersion <= 19) {
this._ref.scrollTo(0, x, true); // use old syntax
} else {
this._ref.scrollTo({x: this.state.width * index, y: 0, animated: true});
}
this.setState({position: index});
if (isUpdating && this.props.onPositionChanged) {
this.props.onPositionChanged(index);
}
}
_getPosition() {
if (typeof this.props.position === 'number') {
return this.props.position;
}
return this.state.position;
}
componentDidUpdate(prevProps) {
if (prevProps.position !== this.props.position) {
this._move(this.props.position);
}
}
componentWillMount() {
const width = this.state.width;
let release = (e, gestureState) => {
const width = this.state.width;
const relativeDistance = gestureState.dx / width;
const vx = gestureState.vx;
let change = 0;
if (relativeDistance < -0.5 || (relativeDistance < 0 && vx <= 0.5)) {
change = 1;
} else if (relativeDistance > 0.5 || (relativeDistance > 0 && vx >= 0.5)) {
change = -1;
}
const position = this._getPosition();
if (position === 0 && change === -1) {
change = 0;
} else if (position + change >= this.props.images.length) {
change = (this.props.images.length) - (position + change);
}
this._move(position + change);
return true;
};
this._panResponder = PanResponder.create({
onStartShouldSetPanResponder: () => true,
onMoveShouldSetPanResponder: () => true,
onPanResponderRelease: release,
onPanResponderTerminate: release,
});
this._interval = setInterval(() => {
const newWidth = Dimensions.get('window').width;
if (newWidth !== this.state.width) {
this.setState({width: newWidth});
}
}, 16);
}
componentWillUnmount() {
clearInterval(this._interval);
}
render() {
const width = this.state.width;
const height = this.props.height || this.state.height;
const position = this._getPosition();
return (<View>
<ScrollView
ref={ref => this._onRef(ref)}
decelerationRate={0.99}
horizontal={true}
showsHorizontalScrollIndicator={false}
style={[styles.container, this.props.style, {height: height}]}>
<View {...this._panResponder.panHandlers} style={styles.containerView}>
{this.props.images.map((image, index) => {
const imageObject = typeof image === 'string' ? {uri: image} : image;
const imageComponent = <Image
key={index}
source={imageObject}
style={{height, width}}
/>;
if (this.props.onPress) {
return (
<TouchableOpacity
key={index}
style={{height, width}}
onPress={() => this.props.onPress({image, index})}
delayPressIn={200}
>
{imageComponent}
</TouchableOpacity>
);
} else {
return imageComponent;
}
})}
</View>
</ScrollView>
<View style={styles.buttons}>
{this.props.images.map((image, index) => {
return (<TouchableHighlight
key={index}
underlayColor="#ccc"
onPress={() => {
return this._move(index);
}}
style={[styles.button, position === index && styles.buttonSelected]}>
<View></View>
</TouchableHighlight>);
})}
</View>
</View>);
}
}