forked from lookingstars/RNMeituan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCloverSlider.js
193 lines (161 loc) · 4.87 KB
/
CloverSlider.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
/*
* 源码来自https://github.com/hugohua/react-native-demo,有点小修改
*/
'use strict';
var React = require('react-native');
var TimerMixin = require('react-timer-mixin');
var Dimensions = require('Dimensions');
var {
StyleSheet,
View,
Text,
Image,
ScrollView,
} = React;
//获取可视窗口的宽高
var { width, height, scale } = Dimensions.get('window');
var itemHeight = 100,
picFormat = '_640x200xzq75.jpg';
//mui 3.0 slider 规范
//TODO 这种方式不够科学,目前只是实现效果,后续请@遂宇做优化吧
//IP6
if(height === 375){
itemHeight = 117;
picFormat = '_750x234xzq75.jpg';
}else if(height === 414){ //IP6 Plug
itemHeight = 99.6;
picFormat = '_1080x260xzq75.jpg';
}
var styles = StyleSheet.create({
container: {
flex: 1
},
pageIndicator: {
position : 'absolute',
backgroundColor : 'transparent',
left : 12,
bottom : -10,
flexDirection: 'row'
}
});
module.exports = React.createClass({
mixins: [TimerMixin],
//默认值
getDefaultProps() {
return {
width: width,
indicatorColor: '#ffffff',
inactiveIndicatorColor: '#ffffff',
timer : 5000,
api : 'http://ald.taobao.com/recommend.htm?appId=lb-tms-1261576-40550'
}
},
//初始化用于状态转换的值
getInitialState() {
return {
currentX: 0,
activePage: 0,
dataSource : []
}
},
//拉取投放的数据
fetchData() {
var me = this;
fetch(me.props.api)
.then((response) => response.json())
.then((responseData) => {
me.setState({
dataSource: responseData.data
});
})
.done(function(){
me.start();
});
},
start(){
var scrollView = this.refs.scrollView;
var length = this.state.dataSource.length;
this.timer = this.setInterval(function(){
var activePage;
if( (this.state.activePage + 1) >= length){
activePage = 0;
}else{
activePage = this.state.activePage + 1;
}
var currentX = this.props.width * activePage;
scrollView.scrollResponderScrollTo(currentX, 0);
this.setState({
currentX: currentX,
activePage: activePage
});
}, this.props.timer)
},
componentDidMount() {
this.fetchData();
},
//TODO 开始滚动时清除timer
_onScrollBegin(event) {
this.clearInterval(this.timer);
},
_onScrollEnd() {
},
getImage:function(url): string{
// return ('http://p0.meituan.net/200.120/deal/667c7aa92a0c04779e266bbfa7d77c64316233.jpg');
if (url.search('https:') === -1) {
return ('https:' + url);
}else{
return (url);
}
},
//渲染单个图片
renderItems(data) {
var weakself = this;
return data.map(function(item,i){
var imgurl = weakself.getImage(item.img);
return(
<Image key={i} style={{width: width,height:itemHeight}} source={{uri: imgurl + picFormat}}/>
);
})
},
render() {
var data = this.state.dataSource
return (
<View style={styles.container}>
<ScrollView
ref='scrollView'
contentContainerStyle={styles.container}
automaticallyAdjustContentInsets={false}
horizontal={true}
pagingEnabled={true}
showsHorizontalScrollIndicator={false}
onMomentumScrollEnd={this.onAnimationEnd}
// onScrollBeginDrag={this._onScrollBegin}
>
{this.renderItems(data)}
</ScrollView>
{this.renderPageIndicator()}
</View>
);
},
renderPageIndicator() {
var indicators = [],
style;
for (var i=0; i< this.state.dataSource.length; i++) {
style = i === this.state.activePage ? { color: this.props.indicatorColor,opacity : 1 } : { color: this.props.inactiveIndicatorColor,opacity : 0.3 };
indicators.push(<Text key={i} style={[style, {fontSize: 32}]}>•</Text>)
}
return (
<View style={styles.pageIndicator}>
{indicators}
</View>
)
},
onAnimationEnd(e) {
var activePage = e.nativeEvent.contentOffset.x / this.props.width;
// console.log(e.nativeEvent)
this.setState({
currentX: e.nativeEvent.contentOffset.x,
activePage: activePage
});
}
});