Skip to content

Commit 87ab19a

Browse files
authored
Autoplay functionality
Autoplay functionality with timeout timing.
1 parent 0322e15 commit 87ab19a

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import Swiper from "react-native-custom-swiper";
1818
<Swiper
1919
style={{ flex: 1 }}
2020
currentSelectIndex={0}
21+
autoplay={true}
22+
autoplayTimeout={2}
2123
swipeData={this.state.imgArray}
2224
renderSwipeItem={this.renderImageSwipeItem}
2325
onScreenChange={this.screenChange}
@@ -77,6 +79,8 @@ render() {
7779
</View>
7880
<Swiper
7981
currentSelectIndex={0}
82+
autoplay={true}
83+
autoplayTimeout={2}
8084
backgroundColor="rgb(221,244,253)"
8185
swipeData={this.state.imgArray}
8286
renderSwipeItem={this.renderImageSwipeItem}

Swiper.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ const { width, height } = Dimensions.get("window");
88
const leftArrow = require("./resource/leftIcon.png");
99
const rightArrow = require("./resource/rightIcon.png");
1010

11+
autoplayTimer = null;
12+
1113
class Swiper extends React.Component {
1214
constructor(props) {
1315
super(props);
1416
this.state = {
1517
showSwipeBtn: this.props.showSwipeBtn,
18+
autoplay : this.props.autoplay,
19+
autoplayTimeout : this.props.autoplayTimeout,
1620
arrSwipeData: this.props.swipeData,
1721
currentSelectIndex:
1822
this.props.currentSelectIndex < this.props.swipeData.length
@@ -24,12 +28,19 @@ class Swiper extends React.Component {
2428
this.viewabilityConfig = { viewAreaCoveragePercentThreshold: 50 };
2529
}
2630

31+
32+
componentWillReceiveProps (nextProps) {
33+
clearTimeout(autoplayTimer)
34+
}
35+
2736
componentDidMount = () => {
2837
if (this.swiper) {
2938
this.swiper.scrollToIndex({
3039
animated: false,
3140
index: this.state.currentSelectIndex,
3241
});
42+
43+
this.autoplay()
3344
}
3445
};
3546

@@ -41,6 +52,7 @@ class Swiper extends React.Component {
4152
index: this.state.currentSelectIndex + 1,
4253
animated: true,
4354
});
55+
this.autoplay()
4456
}
4557
};
4658

@@ -50,6 +62,7 @@ class Swiper extends React.Component {
5062
index: this.state.currentSelectIndex - 1,
5163
animated: true,
5264
});
65+
this.autoplay()
5366
}
5467
};
5568

@@ -59,9 +72,29 @@ class Swiper extends React.Component {
5972
this.setState({
6073
currentSelectIndex: viewableItems[0].index,
6174
});
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+
6285
}
6386
};
6487

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+
6598
getItemLayout = (data, index) => ({
6699
length: this.props.containerWidth,
67100
offset: this.props.containerWidth * index,
@@ -147,6 +180,8 @@ Swiper.propTypes = {
147180
leftButtonImage: PropTypes.any,
148181
rightButtonImage: PropTypes.any,
149182
showSwipeBtn: PropTypes.bool,
183+
autoplay: PropTypes.bool,
184+
autoplayTimeout: PropTypes.number,
150185
currentSelectIndex: PropTypes.number,
151186
containerWidth: PropTypes.number,
152187
style: PropTypes.object,
@@ -159,7 +194,9 @@ Swiper.defaultProps = {
159194
leftButtonImage: leftArrow,
160195
rightButtonImage: rightArrow,
161196
showSwipeBtn: true,
197+
autoplayTimeout:5000,
162198
currentSelectIndex: 0,
199+
autoplay: false,
163200
containerWidth: width,
164201
style: { flex: 1, backgroundColor: "white" },
165202
onScreenChange: () => {},

0 commit comments

Comments
 (0)