@@ -8,11 +8,15 @@ const { width, height } = Dimensions.get("window");
8
8
const leftArrow = require ( "./resource/leftIcon.png" ) ;
9
9
const rightArrow = require ( "./resource/rightIcon.png" ) ;
10
10
11
+ autoplayTimer = null ;
12
+
11
13
class Swiper extends React . Component {
12
14
constructor ( props ) {
13
15
super ( props ) ;
14
16
this . state = {
15
17
showSwipeBtn : this . props . showSwipeBtn ,
18
+ autoplay : this . props . autoplay ,
19
+ autoplayTimeout : this . props . autoplayTimeout ,
16
20
arrSwipeData : this . props . swipeData ,
17
21
currentSelectIndex :
18
22
this . props . currentSelectIndex < this . props . swipeData . length
@@ -24,12 +28,19 @@ class Swiper extends React.Component {
24
28
this . viewabilityConfig = { viewAreaCoveragePercentThreshold : 50 } ;
25
29
}
26
30
31
+
32
+ componentWillReceiveProps ( nextProps ) {
33
+ clearTimeout ( autoplayTimer )
34
+ }
35
+
27
36
componentDidMount = ( ) => {
28
37
if ( this . swiper ) {
29
38
this . swiper . scrollToIndex ( {
30
39
animated : false ,
31
40
index : this . state . currentSelectIndex ,
32
41
} ) ;
42
+
43
+ this . autoplay ( )
33
44
}
34
45
} ;
35
46
@@ -41,6 +52,7 @@ class Swiper extends React.Component {
41
52
index : this . state . currentSelectIndex + 1 ,
42
53
animated : true ,
43
54
} ) ;
55
+ this . autoplay ( )
44
56
}
45
57
} ;
46
58
@@ -50,6 +62,7 @@ class Swiper extends React.Component {
50
62
index : this . state . currentSelectIndex - 1 ,
51
63
animated : true ,
52
64
} ) ;
65
+ this . autoplay ( )
53
66
}
54
67
} ;
55
68
@@ -59,9 +72,29 @@ class Swiper extends React.Component {
59
72
this . setState ( {
60
73
currentSelectIndex : viewableItems [ 0 ] . index ,
61
74
} ) ;
75
+ let that = this ;
76
+ // setTimeout(function(){
77
+ // if (that.state.currentSelectIndex < that.state.arrSwipeData.length - 1) {
78
+ // that.swiper.scrollToIndex({
79
+ // index: that.state.currentSelectIndex + 1,
80
+ // animated: true,
81
+ // });
82
+ // }
83
+ // }, 5000);
84
+
62
85
}
63
86
} ;
64
87
88
+ autoplay = ( ) => {
89
+ if ( this . state . autoplay ) {
90
+ clearInterval ( this . _interval )
91
+ this . _interval = setInterval ( ( ) => {
92
+ this . _onPressNextBtn ( )
93
+ } , this . props . autoplayTimeout * 1000 ) ;
94
+ }
95
+
96
+ }
97
+
65
98
getItemLayout = ( data , index ) => ( {
66
99
length : this . props . containerWidth ,
67
100
offset : this . props . containerWidth * index ,
@@ -147,6 +180,8 @@ Swiper.propTypes = {
147
180
leftButtonImage : PropTypes . any ,
148
181
rightButtonImage : PropTypes . any ,
149
182
showSwipeBtn : PropTypes . bool ,
183
+ autoplay : PropTypes . bool ,
184
+ autoplayTimeout : PropTypes . number ,
150
185
currentSelectIndex : PropTypes . number ,
151
186
containerWidth : PropTypes . number ,
152
187
style : PropTypes . object ,
@@ -159,7 +194,9 @@ Swiper.defaultProps = {
159
194
leftButtonImage : leftArrow ,
160
195
rightButtonImage : rightArrow ,
161
196
showSwipeBtn : true ,
197
+ autoplayTimeout :5000 ,
162
198
currentSelectIndex : 0 ,
199
+ autoplay : false ,
163
200
containerWidth : width ,
164
201
style : { flex : 1 , backgroundColor : "white" } ,
165
202
onScreenChange : ( ) => { } ,
0 commit comments