轮播图,这个东东用的不是一般的多呀,网上有很多轮子,像:三张图/N+1张图/多张图实现无限轮播,起初作为屌丝的我,也是直接百度了一个轮子,放进项目直接使用,起初没有发现坑,正在某个上午我正在欣赏大牛技术帖子的时候,测试过来,说我写的轮播有坑,看了一会,居然发现真的有时候会由最后一张闪一下,到第一张,发现坑后,心想这尼玛,这是谁呀,写了带坑的轮子,让我情何以堪,于是乎,又百度了几个,下载下来一看,不是里面内容冗杂,就是轮子里面有Bug,这完全不符合我的要求呀,所以我决定,即使用一下午的时间,也要自己写一个内容简洁易懂,没有Bug的轮播图,于是造轮子开始。
轮子来也
1-分析三张图实现无限轮播的思路:
既然是三张图片,那在实现的过程中,为了可以左右滑动,都有图片,所以初始化的时候,要让 UIScrollView 的偏移量为自身的宽度,而且不管是滑像上一张还是下一张,为了可以正常进行左右滑动,在滑动结束的时候,都要让 UIScrollView 的偏移量为自身的宽度,所以在 UIScrollView 滑动完一个图片之后,都要将 UIScrollView 的偏移量设置为自身宽度
#pragma mark--懒加载初始化 View
-(UIScrollView *)baseScrollV{
if (!_baseScrollV) {
_baseScrollV =[[UIScrollView alloc]initWithFrame:self.bounds];
_baseScrollV.contentSize =CGSizeMake(BASE_WIDTH*3, BASE_HEIGHT);
_baseScrollV.contentOffset = CGPointMake(BASE_WIDTH, 0);
_baseScrollV.delegate = self;
_baseScrollV.pagingEnabled = YES;
_baseScrollV.showsHorizontalScrollIndicator = NO;
}
return _baseScrollV;
}
-(UIPageControl *)basePageC{
if (!_basePageC) {
_basePageC = [[UIPageControl alloc]initWithFrame:CGRectMake(BASE_WIDTH/2-50, BASE_HEIGHT-50, 100, 50)];
_basePageC.numberOfPages = _imageArrays.count;
_basePageC.currentPageIndicatorTintColor = self.currentColor;
_basePageC.pageIndicatorTintColor = self.normalColor;
_basePageC.userInteractionEnabled = NO;
_basePageC.currentPage = self.currentPageInteger;
}
return _basePageC;
}
-(UIImageView *)leftImageView{
if (!_leftImageView) {
NSInteger index = (_currentPageInteger - 1 + _imageArrays.count) % _imageArrays.count;
_leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,BASE_WIDTH, BASE_HEIGHT)];
_leftImageView.image = _imageArrays[index];
_leftImageView.contentMode = UIViewContentModeScaleAspectFill;
_leftImageView.userInteractionEnabled = YES;
}
return _leftImageView;
}
-(UIImageView *)centerImageView{
if (!_centerImageView) {
_centerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(BASE_WIDTH, 0,BASE_WIDTH, BASE_HEIGHT)];
_centerImageView.image = _imageArrays[self.currentPageInteger];
_centerImageView.contentMode = UIViewContentModeScaleAspectFill;
_centerImageView.userInteractionEnabled = YES;
}
return _centerImageView;
}
-(UIImageView *)rightImageView{
if (!_rightImageView) {
NSInteger index = (_currentPageInteger + 1 + _imageArrays.count) % _imageArrays.count;
_rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(BASE_WIDTH*2, 0,BASE_WIDTH, BASE_HEIGHT)];
_rightImageView.image = _imageArrays[index];
_rightImageView.contentMode = UIViewContentModeScaleAspectFill;
_rightImageView.userInteractionEnabled = YES;
}
return _rightImageView;
}
2-既然总的实现思路已经有了,剩下的就是实现过程中的数据变换了
#pragma mark--ScrollView的代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint currentOffset = self.baseScrollV.contentOffset;
if (currentOffset.x <= 0) {
//上一页
self.currentPageInteger = (self.currentPageInteger - 1 + _imageArrays.count) % _imageArrays.count;
[self resetContent];
} else if (currentOffset.x >= BASE_WIDTH*2) {
//下一页
_currentPageInteger = (_currentPageInteger + 1) % _imageArrays.count;
[self resetContent];
}
}
#pragma mark-- 重置数据
- (void)resetContent{
self.basePageC.currentPage = _currentPageInteger;
//重置偏移量
CGPoint offset = CGPointMake(BASE_WIDTH, 0);
[self.baseScrollV setContentOffset:offset];
//重置图片
NSInteger leftIndex = (_currentPageInteger - 1 + _imageArrays.count) % _imageArrays.count;
NSInteger centerIndex = _currentPageInteger;
NSInteger rightIndex = (_currentPageInteger + 1 + _imageArrays.count) % _imageArrays.count;
self.leftImageView.image = _imageArrays[leftIndex];
self.centerImageView.image = _imageArrays[centerIndex];
当 UIScrollView 的偏移量为 0 时,视图当前的图片序号减一,这里之所以
self.currentPageInteger - 1 + _imageArrays.count
这样写,是为了防止当前序号为 0 时,是负数,产生数组越界的情况
3-最后一步,屌丝逆袭的时候到了——添加定时器
在使用定时器的时候,要注意⚠️:
(1)不能影响定时器的准确性
(2)在手指进行滑动的时候,要暂停或者消灭定时器,在手指放开之后,要启动定时器
鉴于这两点,我把定时器加在 RunLoop中,保证定时器的准确性,在使用中,设置拖拽或者正在动的过程中忽略定时器,实现和手拖动图片不冲突
- (void)timerAction{
if (self.baseScrollV.isDragging || self.baseScrollV.isDecelerating) {
return;
}else {
CGPoint offset = CGPointMake(BASE_WIDTH * 2, 0);
[self.baseScrollV setContentOffset:offset animated:YES];
}
}
// 滑动视图,当手指离开屏幕那一霎那,调用该方法
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[self timerDelay:self.timerInterval];
}
装逼结束,开启使用模式。