iOS无限轮播图 无限复用

关于轮播图的实现, zz所了解到的有三种:

  1. 利用scrollView根据轮播图片的多少来创建imageview 然后添加到scrollview, 设置scrollview滑动大小, 注意的是最后一张图片的后面要再加一张我们要轮播的第一张图片, 同理第一张前面也要加我们要轮播的最后一张图片
    2.同样也是利用scrollView, 这里要先说的就是上面的那种方法的缺陷, 如果要有一百张图片要轮播呢(zz也没有遇到过轮播那多的)道理就是上面的方法不是特别的合理, 我们可以利用scrollview创建三张imageview 一次添加到scrollview上, 我们始终让scrollview显示中间的一张, 当向右滑动的时候, 我们把中间的图片换成右边的, 右边的那张化成右边当前的下一张, 然后我们还是显示中间的哪一张, 同理向左边滑动也是(只不过是改变左边跟中间的图片), 这样我们就实现了复用(这次主要是说的这个)
    3.利用collection view设置他横向滑动...(zz并没有利用这个实现过, 等以后有功夫了学习一下再)
    经过上次一个朋友的建议学了下代码框的使用...markdown也比较好用吧... (zz只是知道了代码框这个东西..两个顿号隔开写代码)(好羡慕那些老司机写出来的东西条理清晰, 并且看着舒服的简书, 毕竟我潜水简书已经快两年了, 但是我真的没写过几次简书)废话不说上代码....
    第一种方式的代码估计大家都会, 我直接上第二种的吧,

...经过上次大神的指点终于会用代码框了
新建一个类, 名字随便吧, 反正我可耻的用了我自己的名字叫ZZCycle
我在ZZCycle.h中这样写

@interface ZZCyCle : UIView
//点击图片触发的回调
@property (nonatomic, copy) void(^myblock)(NSInteger tap);
//重写初始化方法, 注意下就是数组是网络请求图片的url数组
- (id)initWithImageUrl:(NSMutableArray *)imageUrlImage imageChangeTime:(NSTimeInterval)timeInterval Frame:(CGRect)frame;
- (void)removeTimer;
@end
ZZCycle.m中是这样 先看属性设置,
#import "ZZCyCle.h"
//用的afn请求图片
#import <UIImageView+AFNetworking.h>
@interface ZZCyCle ()<UIScrollViewDelegate>
// 图片的接口地址数组
@property (nonatomic, strong) NSMutableArray *imageUrlImage;
// 自动轮播图片的时间
@property (nonatomic, assign) NSInteger timerInterval;
//底部的scrollView
@property (nonatomic, strong) UIScrollView *scrollView;
//scrollView上左边的imageView
@property (nonatomic, strong) UIImageView *leftView;
//scrollView上中间的imageView
@property (nonatomic, strong) UIImageView *currentView;
//scrollView上右边的imageView
@property (nonatomic, strong) UIImageView *rightView;
//左边的imageView在数组中对应url下标
@property (nonatomic, assign) NSInteger leftIndex;
//中间的imageView在数组中对应url下标
@property (nonatomic, assign) NSInteger currentIndex;
//右边的imageView在数组中对应url下标
@property (nonatomic, assign) NSInteger rightIndex;
//轮播图上的小圆点
@property (nonatomic, strong) UIPageControl *pageControl;
//自动轮播的定时器
@property (nonatomic, strong) NSTimer *myTimer;
//判断是否拖拽
@property (nonatomic, assign) BOOL isDrag;

@end

- (NSMutableArray *)imageUrlImage { if (_imageUrlImage == nil ) { self.imageUrlImage = [NSMutableArray array]; } return _imageUrlImage; }
- (id)initWithImageUrl:(NSMutableArray *)imageUrlImage imageChangeTime:(NSTimeInterval)timeInterval Frame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // 两个属性赋值, 方便下面调用 self.imageUrlImage = imageUrlImage; self.timerInterval = timeInterval; // scroll的初始化 [self initWithScroll]; // pageControl初始化 [self initWithPageControl]; // 定时器的初始化 [self initWithMytimer]; } return self; }
//初始化定时器
- (void)initWithMytimer { self.myTimer = [NSTimer scheduledTimerWithTimeInterval:self.timerInterval target:self selector:@selector(changeScrollView) userInfo:nil repeats:YES]; }
//初始化scrollView
- (void)changeScrollView { [self imageChange]; [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width * 2, 0) animated:YES]; }
//将view转化成image
- (UIImage*) imageWithUIView:(UIView*) view { UIGraphicsBeginImageContext(view.bounds.size); CGContextRef context = UIGraphicsGetCurrentContext(); [view.layer renderInContext:context]; UIImage* tImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return tImage; }
//在轮播的界面移除定时器的方法
- (void)removeTimer { [self.myTimer invalidate]; self.myTimer = nil; }

//初始化pageControl
- (void)initWithPageControl { self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(self.bounds.size.width / 2 - 40, self.bounds.size.height - 100, 80, 30)]; self.pageControl.numberOfPages = self.imageUrlImage.count; [self.pageControl addTarget:self action:@selector(valueChange) forControlEvents:UIControlEventValueChanged]; [self addSubview:self.pageControl]; UIView *currentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)]; currentView.backgroundColor = [UIColor blackColor]; UIImage *currentImage = [self imageWithUIView:currentView]; UIView *otherView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)]; otherView.backgroundColor = [UIColor redColor]; UIImage *otherImage = [self imageWithUIView:otherView]; [_pageControl setValue:currentImage forKey:@"_currentPageImage"]; [_pageControl setValue:otherImage forKey:@"_pageImage"]; }
- (void)valueChange { [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width * self.pageControl.currentPage, 0) animated:YES]; }
//初始化scrollView及上边的视图
- (void)initWithScroll { self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds]; self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * 3, self.scrollView.frame.size.height); [self addSubview:self.scrollView]; self.scrollView.delegate = self; self.scrollView.pagingEnabled = YES; [self addSubview:self.scrollView]; // 让scrollView始终在中间图片 [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width, 0)];
// 三张图片在数组中的位置 self.currentIndex = 0; self.leftIndex = self.imageUrlImage.count - 1; self.rightIndex = 1; // 左边的图片 self.leftView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)]; [self.leftView setImageWithURL:[NSURL URLWithString:[self.imageUrlImage objectAtIndex:self.imageUrlImage.count - 1]]]; // 当前的图片 self.currentView = [[UIImageView alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)]; [self.currentView setImageWithURL:[NSURL URLWithString:[self.imageUrlImage objectAtIndex:0]]]; // 右边的图片 self.rightView = [[UIImageView alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width * 2, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)]; [self.rightView setImageWithURL:[NSURL URLWithString:[self.imageUrlImage objectAtIndex:1]]]; [self.scrollView addSubview:self.leftView]; [self.scrollView addSubview:self.rightView]; [self.scrollView addSubview:self.currentView]; self.currentView.userInteractionEnabled = YES; self.rightView.userInteractionEnabled = YES; self.leftView.userInteractionEnabled = YES; UITapGestureRecognizer *currenttap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(currenttap)]; [self.currentView addGestureRecognizer:currenttap]; UITapGestureRecognizer *rightTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rightTap)]; [self.rightView addGestureRecognizer:rightTap]; UITapGestureRecognizer *leftTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(leftTap)]; [self.leftView addGestureRecognizer:leftTap]; }
- (void)leftTap { if (self.myblock) { self.myblock(self.rightIndex); } }
- (void)rightTap { if (self.myblock) { self.myblock(self.rightIndex); } }
- (void)currenttap { if (self.myblock) { self.myblock(self.currentIndex); } }

pragma mark - UIScrollViewDelegate

//scrollView的contentOfSet发生变化
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (self.isDrag) { self.pageControl.currentPage = self.currentIndex; } }
// 在scrollView将要开始拖拽的时候暂时器暂停
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { self.isDrag = YES; // 暂停定时器的方法 [self.myTimer setFireDate:[NSDate distantFuture]]; }

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { [self imageChange]; self.isDrag = NO; // 打开定时器 [self.myTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:self.timerInterval]]; }

//scrollView的contenofset发生变化对应图片的处理的方法
- (void)imageChange {
// 根据index找到对应的图片 之前先判断index的变化 // 先判断中间的index // 向右滚动
if (self.scrollView.contentOffset.x == self.scrollView.frame.size.width * 2) { if (self.currentIndex == self.imageUrlImage.count - 1) { self.currentIndex = 0; } else { self.currentIndex += 1; } }
// 向左滚动 if (self.scrollView.contentOffset.x == 0 ) { if (self.currentIndex == 0 ) { self.currentIndex = self.imageUrlImage.count - 1; } else { self.currentIndex--; } }
// 在判断右边的index if (self.currentIndex == self.imageUrlImage.count - 1) { self.rightIndex = 0; } else { self.rightIndex = self.currentIndex + 1; }
// 判断左边的index if (self.currentIndex == 0) { self.leftIndex = self.imageUrlImage.count - 1; } else { self.leftIndex = self.currentIndex - 1; }
// 换图片 [self.currentView setImageWithURL:[NSURL URLWithString:[self.imageUrlImage objectAtIndex:self.currentIndex]]]; [self.rightView setImageWithURL:[NSURL URLWithString:[self.imageUrlImage objectAtIndex:self.rightIndex]]]; [self.leftView setImageWithURL:[NSURL URLWithString:[self.imageUrlImage objectAtIndex:self.leftIndex]]];
[self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width, 0) animated:NO]; if (!self.isDrag) { if (self.pageControl.currentPage == self.imageUrlImage.count - 1) { self.pageControl.currentPage = 0; } else { self.pageControl.currentPage++; } } }

完工...好像我把整个demo粘上去了, 注释上面都清楚, 请各位大虾指教...

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 156,265评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,274评论 1 288
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,087评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,479评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,782评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,218评论 1 207
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,594评论 2 309
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,316评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,955评论 1 237
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,274评论 2 240
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,803评论 1 255
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,177评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,732评论 3 229
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,953评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,687评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,263评论 2 267
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,189评论 2 258

推荐阅读更多精彩内容