-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRNScrollViewDemo.js
188 lines (157 loc) · 4.21 KB
/
RNScrollViewDemo.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
import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Image,
ScrollView,
Dimensions,
Platform
} from 'react-native';
var {width} = Dimensions.get('window');
const ios = Platform.OS;
class RNScrollViewDemo extends Component {
constructor(props) {
super(props);
this.state = {
currentPage: 0,
duration: 2000 //每隔一秒开始轮播
};
}
render() {
return (
<View style={styles.container}>
<ScrollView
ref="scrollView"
horizontal={true}
showsHorizontalScrollIndicator={false}
pagingEnabled={true}
onMomentumScrollEnd={(scrollView)=>this.onAnimationEnd(scrollView)}
onScrollBeginDrag={this.onScrollBeginDrag.bind(this)}
onScrollEndDrag={this.onScrollEndDrag.bind(this)}
>
{this._renderAllImage()}
</ScrollView>
<View style={styles.circleContainer}>
{this._renderCircleIndicator()}
</View>
</View>
);
}
/**
* 一般在此方法中处理一些耗时操作
*/
componentDidMount() {
this._startTimer();
}
componentWillUnMount() {
clearInterval(this.interval);
}
/**
* 演染图片
* @returns {Array}
* @private
*/
_renderAllImage() {
var imgArr = [];
var imgData = this.props.imgData;
for (var i in imgData) {
imgArr.push(
<Image
key={i}
source={{uri: imgData[i].icon}}
style={{width: width, height: 140}}
/>
)
}
return imgArr;
}
/**
* 渲染圆点指示器
* @private
*/
_renderCircleIndicator() {
var circleArr = [];
var imgData = this.props.imgData;
var style;
for (var i in imgData) {
style = i == this.state.currentPage ? {color: 'orange'} : {color: 'white'};
circleArr.push(
<Text key={i} style={[{fontSize: 25}, style]}>•</Text>
);
}
return circleArr;
}
/**
* 当一页滑动结束时调用
* @param scrollView
*/
onAnimationEnd(scrollView) {
// 计算一页滑动的偏移量
var offSetX = scrollView.nativeEvent.contentOffset.x;
console.log(offSetX);
// 算出当前为第几页
var currentPage = Math.floor((offSetX / width));
this.setState({
currentPage: currentPage
});
}
/**
* 开始拖拽时的回调
* @private
*/
onScrollBeginDrag() {
clearInterval(this.interval);
}
/**
* 拖拽停止时的回调
* @private
*/
onScrollEndDrag() {
this._startTimer();
}
/**
* 开启定时器
* @private
*/
_startTimer() {
var scrollView = this.refs.scrollView;
var imgCount = this.props.imgData.length;
this.interval = setInterval(() => {
//记录当前正在活动的图片
var activePage = 0;
if ((this.state.currentPage + 1) >= imgCount) { //防止越界
activePage = 0;
} else {
activePage = this.state.currentPage + 1;
}
this.setState({
currentPage: activePage
});
//让ScrollView动起来
var offSetX = activePage * width;
scrollView.scrollResponderScrollTo({x: offSetX, y: 0, animated: true});
}, this.state.duration);
}
}
const styles = StyleSheet.create({
container: {
marginTop: ios == 'ios' ? 25 : 0
},
circleContainer: {
width: width,
height: 25,
flexDirection: 'row',
backgroundColor: 'rgba(0,0,0,0.4)',
alignItems: 'center',
position: 'absolute',
bottom: 0
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
module.exports = RNScrollViewDemo;