设置三个ImageView
- (void)setScrollViewAndPageControl {
self.scrollView.contentSize = CGSizeMake(viewWidth*3, viewHeight);
self.pageControl.userInteractionEnabled = NO;
_firstView = [[UIImageView alloc]init];
_middleView = [[UIImageView alloc]init];
_lastView = [[UIImageView alloc]init];
_firstView.frame = CGRectMake(0, 0, viewWidth, viewHeight);
_middleView.frame = CGRectMake(viewWidth, 0, viewWidth, viewHeight);
_lastView.frame = CGRectMake(viewWidth*2, 0, viewWidth, viewHeight);
[_scrollView addSubview:_firstView];
[_scrollView addSubview:_middleView];
[_scrollView addSubview:_lastView];
_currentPage = 0;
}
刷新
- (void)reloadData {
CGFloat contentOffSetX = self.scrollView.contentOffset.x;
NSInteger imgCount = self.imgArray.count;
NSInteger leftImgIndex;
NSInteger rightImgIndex;
if (contentOffSetX < viewWidth) {
self.currentPage = (self.currentPage + imgCount - 1) % imgCount;
} else if (contentOffSetX > viewWidth) {
self.currentPage = (self.currentPage + 1) % imgCount;
}
leftImgIndex = (self.currentPage + imgCount - 1) % imgCount;
rightImgIndex = (self.currentPage + 1) % imgCount;
NSString *middelImgStr = self.imgArray[self.currentPage][@"uiIconUrl"];
NSString *firstImgStr = self.imgArray[leftImgIndex][@"uiIconUrl"];
NSString *lastImgStr = self.imgArray[rightImgIndex][@"uiIconUrl"];
[_middleView sd_setImageWithURL:[NSURL URLWithString:middelImgStr] placeholderImage:nil];
[_firstView sd_setImageWithURL:[NSURL URLWithString:firstImgStr] placeholderImage:nil];
[_lastView sd_setImageWithURL:[NSURL URLWithString:lastImgStr] placeholderImage:nil];
}
定时器
- (void)setRollScrollView {
self.imgListTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
dispatch_source_set_timer(self.imgListTimer, DISPATCH_TIME_NOW, 5.0 * NSEC_PER_SEC, 0.0 * NSEC_PER_SEC);
dispatch_source_set_event_handler(self.imgListTimer, ^{
[self.scrollView setContentOffset:CGPointMake(viewWidth * 2, 0) animated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self scrollViewDidEndDecelerating:self.scrollView];
});
});
}
至此,大功告成
Ps:有事拍砖