新版无限轮播视图,2个imageView完成(不依赖任何三方库)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了新版无限轮播视图,2个imageView完成(不依赖任何三方库)相关的知识,希望对你有一定的参考价值。
笔者最新改写的无限轮播视图,完全不依赖任何三方库,2个imageView实现无限轮播。
特点:
1.支持图片缓存到本地,亦支持一键清除缓存。
2.超级轻量,没有任何第三方框架参合,占用内存极小
3.高度自定义,支持修改pageControll的显示图片及显示位置
4.图片展示模式多样性,支持轮播展示与渐入渐出模式展示
5.支持自动滚动
6.创建方式多样,支持代码及storyboard创建
7.支持显示图片简介,具体见GitHub效果图
代码实现:
.h
// // WYScrollView.h // WYScrollView // // Created by jacke-xu on 16/6/7. // Copyright © 2016年 jacke-xu. All rights reserved. // #import <UIKit/UIKit.h> @class WYScrollView; //block方式监控图片的点击事件 typedef void(^ClickBlock)(NSInteger index); //pageControl的显示位置 typedef enum { PositionNone, //默认值 == PositionBottomCenter PositionHide, //隐藏 PositionTopCenter, //中上 PositionBottomLeft, //左下 PositionBottomCenter, //中下 PositionBottomRight //右下 } PageControlPosition; //图片切换的方式 typedef enum { ChangeModeDefault, //轮播滚动 ChangeModeFade //淡入淡出 }ChangeMode; //代理方式监控图片的点击事件 @protocol WYScrollViewDelegate <NSObject> /** * 该方法用来处理图片的点击,会返回图片在数组中的索引 * 代理与block二选一即可,若两者都实现,block的优先级高 * * @param scrollView 控件本身 * @param index 图片索引 */ - (void)scrollView:(WYScrollView *)scrollView clickImageAtIndex:(NSInteger)index; @end /** * 说明:要想正常使用,图片数组imageArray必须设置 * 控件的frame必须设置,xib\sb创建的可不设置 * 其他属性都有默认值,可不设置 */ @interface WYScrollView : UIView /* 这里没有提供修改占位图片的接口,如果需要修改,可直接到.m文件中 搜索"WYScrollView"替换为你想要显示的图片名称,或者将原有的占位 图片删除并修改你想要显示的图片名称为"WYScrollView"。 不需要占位图片的请将[UIImage imageNamed:@"WYScrollView"] 修改为[UIImage new]或[[UIImage alloc] init] */ #pragma mark 属性 /** * 设置图片切换的模式,默认为ChangeModeDefault */ @property (nonatomic, assign) ChangeMode changeMode; /** * 设置分页控件位置,默认为PositionBottomCenter * 只有一张图片时,pageControl隐藏 */ @property (nonatomic, assign) PageControlPosition pagePosition; /** * 轮播的图片数组,可以是本地图片(UIImage,不能是图片名称),也可以是网络路径 */ @property (nonatomic, strong) NSArray *imageArray; /** * 图片描述的字符串数组,应与图片顺序对应 * * 图片描述控件默认是隐藏的 * 设置该属性,控件会显示 * 设置为nil或空数组,控件会隐藏 */ @property (nonatomic, strong) NSArray *describeArray; /** * 每一页停留时间,默认为5s,最少2s * 当设置的值小于2s时,则为默认值 */ @property (nonatomic, assign) NSTimeInterval time; /** * 点击图片后要执行的操作,会返回图片在数组中的索引 */ @property (nonatomic, copy) ClickBlock imageClickBlock; /** * 代理,用来处理图片的点击 */ @property (nonatomic, weak) id<WYScrollViewDelegate> delegate; #pragma mark 构造方法 /** * 构造方法 * * @param imageArray 图片数组 * @param describeArray 图片描述数组 * */ - (instancetype)initWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray; - (instancetype)initWithImageArray:(NSArray *)imageArray imageClickBlock:(void(^)(NSInteger index))imageClickBlock; + (instancetype)scrollViewWithImageArray:(NSArray *)imageArray describeArray:(NSArray *)describeArray; #pragma mark 方法 /** * 开启定时器 * 默认已开启,调用该方法会重新开启 */ - (void)startTimer; /** * 停止定时器 * 停止后,如果手动滚动图片,定时器会检查滚动前的定时器状态,判断是否需要重启定时器 */ - (void)stopTimer; /** * 设置分页控件指示器的图片 * 两个图片必须同时设置,否则设置无效 * 不设置则为系统默认 * * @param pageImage 其他页码的图片 * @param currentImage 当前页码的图片 */ - (void)setPageImage:(UIImage *)image andCurrentPageImage:(UIImage *)currentImage; /** * 设置分页控件指示器的颜色 * 不设置则为系统默认 * * @param color 其他页码的颜色 * @param currentColor 当前页码的颜色 */ - (void)setPageColor:(UIColor *)color andCurrentPageColor:(UIColor *)currentColor; /** * 修改图片描述控件的部分属性,不需要修改的传nil * * @param color 字体颜色,默认为[UIColor whiteColor] * @param font 字体,默认为[UIFont systemFontOfSize:13] * @param bgColor 背景颜色,默认为[UIColor colorWithWhite:0 alpha:0.5] */ - (void)setDescribeTextColor:(UIColor *)color font:(UIFont *)font bgColor:(UIColor *)bgColor; /** * 清除沙盒中的图片缓存 */ - (void)clearDiskCache; @end
.m
// // WYScrollView.m // WYScrollView // // Created by jacke-xu on 16/6/7. // Copyright © 2016年 jacke-xu. All rights reserved. // #import "WYScrollView.h" #define DEFAULTTIME 5 #define HORMARGIN 10 #define VERMARGIN 5 #define DES_LABEL_H 20 @interface WYScrollView ()<UIScrollViewDelegate> //轮播的图片数组 @property (nonatomic, strong) NSMutableArray *images; //图片描述控件,默认在底部 @property (nonatomic, strong) UILabel *describeLabel; //滚动视图 @property (nonatomic, strong) UIScrollView *scrollView; //分页控件 @property (nonatomic, strong) UIPageControl *pageControl; //当前显示的imageView @property (nonatomic, strong) UIImageView *currImageView; //滚动显示的imageView @property (nonatomic, strong) UIImageView *otherImageView; //当前显示图片的索引 @property (nonatomic, assign) NSInteger currIndex; //将要显示图片的索引 @property (nonatomic, assign) NSInteger nextIndex; //pageControl图片大小 @property (nonatomic, assign) CGSize pageImageSize; //定时器 @property (nonatomic, strong) NSTimer *timer; //是否启用了定时器 @property (nonatomic, assign) BOOL isTimer; //任务队列 @property (nonatomic, strong) NSOperationQueue *queue; @end @implementation WYScrollView #pragma mark- 初始化方法 //创建用来缓存图片的文件夹 + (void)initialize { NSString *cache = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"WYScrollView"]; BOOL isDir = NO; BOOL isExists = [[NSFileManager defaultManager] fileExistsAtPath:cache isDirectory:&isDir]; if (!isExists || !isDir) { [[NSFileManager defaultManager] createDirectoryAtPath:cache withIntermediateDirectories:YES attributes:nil error:nil]; } } #pragma mark- frame相关 - (CGFloat)height { return self.scrollView.frame.size.height; } - (CGFloat)width { return self.scrollView.frame.size.width; } #pragma mark- 懒加载 - (NSOperationQueue *)queue { if (!_queue) { _queue = [[NSOperationQueue alloc] init]; } return _queue; } - (UIScrollView *)scrollView { if (!_scrollView) { _scrollView = [[UIScrollView alloc] init]; _scrollView.pagingEnabled = YES; _scrollView.bounces = NO; _scrollView.showsHorizontalScrollIndicator = NO; _scrollView.showsVerticalScrollIndicator = NO; _scrollView.delegate = self; //添加手势监听图片的点击 [_scrollView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClick)]]; _currImageView = [[UIImageView alloc] init]; [_scrollView addSubview:_currImageView]; _otherImageView = [[UIImageView alloc] init]; [_scrollView addSubview:_otherImageView]; self.isTimer = YES; } return _scrollView; } - (UILabel *)describeLabel { if (!_describeLabel) { _describeLabel = [[UILabel alloc] init]; _describeLabel.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5]; _describeLabel.textColor = [UIColor whiteColor]; _describeLabel.textAlignment = NSTextAlignmentCenter; _describeLabel.font = [UIFont systemFontOfSize:13]; _describeLabel.hidden = YES; } return _describeLabel; } - (UIPageControl *)pageControl { if (!_pageControl) { _pageControl = [[UIPageControl alloc] init]; _pageControl.userInteractionEnabled = NO; } return _pageControl; } #pragma mark- 构造方法 - (instancetype)initWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray { if (self = [super initWithFrame:frame]) { self.imageArray = imageArray; } return self; } - (instancetype)initWithImageArray:(NSArray *)imageArray imageClickBlock:(void(^)(NSInteger index))imageClickBlock { if (self = [self initWithFrame:CGRectZero imageArray:imageArray]) { self.imageClickBlock = imageClickBlock; } return self; } + (instancetype)scrollViewWithImageArray:(NSArray *)imageArray describeArray:(NSArray *)describeArray { WYScrollView *wyScrollView = [[self alloc] init]; wyScrollView.imageArray = imageArray; wyScrollView.describeArray = describeArray; return wyScrollView; } #pragma mark- --------设置相关方法-------- #pragma mark 设置控件的frame,并添加子控件 - (void)setFrame:(CGRect)frame { [super setFrame:frame]; [self addSubview:self.scrollView]; [self addSubview:self.describeLabel]; [self addSubview:self.pageControl]; } #pragma mark 设置图片数组 - (void)setImageArray:(NSArray *)imageArray{ if (!imageArray.count) return; _imageArray = imageArray; _images = [NSMutableArray array]; for (int i = 0; i < imageArray.count; i++) { if ([imageArray[i] isKindOfClass:[UIImage class]]) { [_images addObject:imageArray[i]]; } else if ([imageArray[i] isKindOfClass:[NSString class]]){ //如果是网络图片,则先添加占位图片,下载完成后替换 [_images addObject:[UIImage imageNamed:@"Placeholder"]]; [self downloadImages:i]; } } //防止在滚动过程中重新给imageArray赋值时报错 if (_currIndex >= _images.count) _currIndex = _images.count - 1; self.currImageView.image = _images[_currIndex]; self.describeLabel.text = _describeArray[_currIndex]; self.pageControl.numberOfPages = _images.count; [self layoutSubviews]; } #pragma mark 设置描述数组 - (void)setDescribeArray:(NSArray *)describeArray{ _describeArray = describeArray; if (!describeArray.count) { _describeArray = nil; self.describeLabel.hidden = YES; } else { //如果描述的个数与图片个数不一致,则补空字符串 if (describeArray.count < _images.count) { NSMutableArray *describes = [NSMutableArray arrayWithArray:describeArray]; for (NSInteger i = describeArray.count; i < _images.count; i++) { [describes addObject:@""]; } _describeArray = describes; } self.describeLabel.hidden = NO; _describeLabel.text = _describeArray[_currIndex]; } //重新计算pageControl的位置 self.pagePosition = self.pagePosition; } #pragma mark 设置scrollView的contentSize - (void)setScrollViewContentSize { if (_images.count > 1) { self.scrollView.contentSize = CGSizeMake(self.width * 5, 0); self.scrollView.contentOffset = CGPointMake(self.width * 2, 0); self.currImageView.frame = CGRectMake(self.width * 2, 0, self.width, self.height); if (_changeMode == ChangeModeFade) { //淡入淡出模式,两个imageView都在同一位置,改变透明度就可以了 _currImageView.frame = CGRectMake(0, 0, self.width, self.height); _otherImageView.frame = self.currImageView.frame; _otherImageView.alpha = 0; [self insertSubview:self.currImageView atIndex:0]; [self insertSubview:self.otherImageView atIndex:1]; } [self startTimer]; } else { //只有一张图片时,scrollview不可滚动,且关闭定时器 self.scrollView.contentSize = CGSizeZero; self.scrollView.contentOffset = CGPointZero; self.currImageView.frame = CGRectMake(0, 0, self.width, self.height); [self stopTimer]; } } #pragma mark 设置图片描述控件 - (void)setDescribeTextColor:(UIColor *)color font:(UIFont *)font bgColor:(UIColor *)bgColor { if (color) self.describeLabel.textColor = color; if (font) self.describeLabel.font = font; if (bgColor) self.describeLabel.backgroundColor = bgColor; } #pragma mark 设置pageControl的指示器图片 - (void)setPageImage:(UIImage *)image andCurrentPageImage:(UIImage *)currentImage { if (!image || !currentImage) return; self.pageImageSize = image.size; [self.pageControl setValue:currentImage forKey:@"_currentPageImage"]; [self.pageControl setValue:image forKey:@"_pageImage"]; } #pragma mark 设置pageControl的指示器颜色 - (void)setPageColor:(UIColor *)color andCurrentPageColor:(UIColor *)currentColor { _pageControl.pageIndicatorTintColor = color; _pageControl.currentPageIndicatorTintColor = currentColor; } #pragma mark 设置pageControl的位置 - (void)setPagePosition:(PageControlPosition)pagePosition { _pagePosition = pagePosition; _pageControl.hidden = (_pagePosition == PositionHide) || (_imageArray.count == 1); if (_pageControl.hidden) return; CGSize size; if (!_pageImageSize.width) {//没有设置图片,系统原有样式 size = [_pageControl sizeForNumberOfPages:_pageControl.numberOfPages]; size.height = 8; } else {//设置图片了 size = CGSizeMake(_pageImageSize.width * (_pageControl.numberOfPages * 2 - 1), _pageImageSize.height); } _pageControl.frame = CGRectMake(0, 0, size.width, size.height); CGFloat centerY = self.height - size.height * 0.5 - VERMARGIN - (_describeLabel.hidden?0: DES_LABEL_H); CGFloat pointY = self.height - size.height - VERMARGIN - (_describeLabel.hidden?0: DES_LABEL_H); if (_pagePosition == PositionNone || _pagePosition == PositionBottomCenter) _pageControl.center = CGPointMake(self.width * 0.5, centerY); else if (_pagePosition == PositionTopCenter) _pageControl.center = CGPointMake(self.width * 0.5, size.height * 0.5 + VERMARGIN); else if (_pagePosition == PositionBottomLeft) _pageControl.frame = CGRectMake(HORMARGIN, pointY, size.width, size.height); else _pageControl.frame = CGRectMake(self.width - HORMARGIN - size.width, pointY, size.width, size.height); } #pragma mark 设置定时器时间 - (void)setTime:(NSTimeInterval)time { _time = time; [self startTimer]; } #pragma mark- --------定时器相关方法-------- - (void)startTimer { //如果只有一张图片,则直接返回,不开启定时器 if (_images.count <= 1) return; //如果定时器已开启,先停止再重新开启 if (self.timer) [self stopTimer]; self.timer = [NSTimer timerWithTimeInterval:_time < 2? DEFAULTTIME: _time target:self selector:@selector(nextPage) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; self.isTimer = YES; } - (void)stopTimer { self.isTimer = NO; [self.timer invalidate]; self.timer = nil; } - (void)nextPage { if (_changeMode == ChangeModeFade) { //淡入淡出模式,不需要修改scrollview偏移量,改变两张图片的透明度即可 self.nextIndex = (self.currIndex + 1) % _images.count; self.otherImageView.image = _images[_nextIndex]; [UIView animateWithDuration:1.2 animations:^{ self.currImageView.alpha = 0; self.otherImageView.alpha = 1; self.pageControl.currentPage = _nextIndex; } completion:^(BOOL finished) { [self changeToNext]; }]; } else [self.scrollView setContentOffset:CGPointMake(self.width * 3, 0) animated:YES]; } #pragma mark- -----------其它----------- #pragma mark 布局子控件 - (void)layoutSubviews { [super layoutSubviews]; //有导航控制器时,会默认在scrollview上方添加64的内边距,这里强制设置为0 _scrollView.contentInset = UIEdgeInsetsZero; _scrollView.frame = self.bounds; _describeLabel.frame = CGRectMake(0, self.height - DES_LABEL_H, self.width, DES_LABEL_H); //重新计算pageControl的位置 self.pagePosition = self.pagePosition; [self setScrollViewContentSize]; } #pragma mark 图片点击事件 - (void)imageClick { if (self.imageClickBlock) { self.imageClickBlock(self.currIndex); } else if ([_delegate respondsToSelector:@selector(scrollView:clickImageAtIndex:)]){ [_delegate scrollView:self clickImageAtIndex:self.currIndex]; } } #pragma mark 下载网络图片 - (void)downloadImages:(int)index { NSString *key = _imageArray[index]; //从沙盒中取图片 NSString *path = [[[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"WYScrollView"] stringByAppendingPathComponent:[key lastPathComponent]]; NSData *data = [NSData dataWithContentsOfFile:path]; if (data) { _images[index] = [UIImage imageWithData:data]; return; }else { //如果沙盒里的图片有不需要显示的,则直接清空沙盒,节约资源 [self clearDiskCache]; } //下载图片 NSBlockOperation *download = [NSBlockOperation blockOperationWithBlock:^{ NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:key]]; if (!data) return; UIImage *image = [UIImage imageWithData:data]; //取到的data有可能不是图片 if (image) { self.images[index] = image; //如果下载的图片为当前要显示的图片,直接到主线程给imageView赋值,否则要等到下一轮才会显示 if (_currIndex == index) [_currImageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO]; [data writeToFile:path atomically:YES]; } }]; [self.queue addOperation:download]; } #pragma mark 清除沙盒中的图片缓存 - (void)clearDiskCache { NSString *cache = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"WYScrollView"]; NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:cache error:NULL]; for (NSString *fileName in contents) { [[NSFileManager defaultManager] removeItemAtPath:[cache stringByAppendingPathComponent:fileName] error:nil]; } } #pragma mark 当图片滚动过半时就修改当前页码 - (void)changeCurrentPageWithOffset:(CGFloat)offsetX { if (offsetX < self.width * 1.5) { NSInteger index = self.currIndex - 1; if (index < 0) index = self.images.count - 1; _pageControl.currentPage = index; } else if (offsetX > self.width * 2.5){ _pageControl.currentPage = (self.currIndex + 1) % self.images.count; } else { _pageControl.currentPage = self.currIndex; } } #pragma mark- --------UIScrollViewDelegate-------- - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (CGSizeEqualToSize(CGSizeZero, scrollView.contentSize)) return; CGFloat offsetX = scrollView.contentOffset.x; //滚动过程中改变pageControl的当前页码 [self changeCurrentPageWithOffset:offsetX]; //向右滚动 if (offsetX < self.width * 2) { if (_changeMode == ChangeModeFade) { self.currImageView.alpha = offsetX / self.width - 1; self.otherImageView.alpha = 2 - offsetX / self.width; } else self.otherImageView.frame = CGRectMake(self.width, 0, self.width, self.height); self.nextIndex = self.currIndex - 1; if (self.nextIndex < 0) self.nextIndex = _images.count - 1; if (offsetX <= self.width) [self changeToNext]; //向左滚动 } else if (offsetX > self.width * 2){ if (_changeMode == ChangeModeFade) { self.otherImageView.alpha = offsetX / self.width - 2; self.currImageView.alpha = 3 - offsetX / self.width; } else self.otherImageView.frame = CGRectMake(CGRectGetMaxX(_currImageView.frame), 0, self.width, self.height); self.nextIndex = (self.currIndex + 1) % _images.count; if (offsetX >= self.width * 3) [self changeToNext]; } self.otherImageView.image = self.images[self.nextIndex]; } - (void)changeToNext { if (_changeMode == ChangeModeFade) { self.currImageView.alpha = 1; self.otherImageView.alpha = 0; } //切换到下一张图片 self.currImageView.image = self.otherImageView.image; self.scrollView.contentOffset = CGPointMake(self.width * 2, 0); self.currIndex = self.nextIndex; self.pageControl.currentPage = self.currIndex; self.describeLabel.text = self.describeArray[self.currIndex]; } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { if(self.isTimer == YES) { [self stopTimer]; self.isTimer = YES; }else { [self stopTimer]; } } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ if(self.isTimer == YES) { [self startTimer]; }else { [self stopTimer]; } } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end
调用方法 :详见代码
// // ViewController.m // WYScrollView // // Created by jacke-xu on 16/6/7. // Copyright © 2016年 jacke-xu. All rights reserved. // #import "ViewController.h" #import "WYScrollView.h" @interface ViewController ()<WYScrollViewDelegate> @property (nonatomic, strong)WYScrollView *scrollView; @property (weak, nonatomic) IBOutlet WYScrollView *scrollView1; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.title = @"测试"; self.view.backgroundColor = [UIColor whiteColor]; //网络图片 NSArray *arr2 = @[@"http://hiphotos.baidu.com/praisejesus/pic/item/e8df7df89fac869eb68f316d.jpg", @"http://pic39.nipic.com/20140226/18071023_162553457000_2.jpg", @"http://file27.mafengwo.net/M00/B2/12/wKgB6lO0ahWAMhL8AAV1yBFJDJw20.jpeg"]; //既有本地图片也有网络图片 NSArray *arr3 = @[@"http://pic39.nipic.com/20140226/18071023_162553457000_2.jpg", [UIImage imageNamed:@"2.jpg"], @"http://hiphotos.baidu.com/praisejesus/pic/item/e8df7df89fac869eb68f316d.jpg", [UIImage imageNamed:@"1.jpg"]]; NSArray *describeArray = @[@"这是第一张图片的描述", @"这是第二张图片的描述", @"这是第三张图片的描述", @"这是第四张图片的描述"]; /** * 通过代码创建 */ self.scrollView = [WYScrollView scrollViewWithImageArray:arr3 describeArray:describeArray]; //设置frame self.scrollView.frame = CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 180); //用代理处理图片点击 self.scrollView.delegate = self; //设置每张图片的停留时间,默认值为5s,最少为2s _scrollView.time = 3; //设置分页控件的图片,不设置则为系统默认 [_scrollView setPageImage:[UIImage imageNamed:@"other"] andCurrentPageImage:[UIImage imageNamed:@"current"]]; //设置分页控件的位置,默认为PositionBottomCenter _scrollView.pagePosition = PositionBottomRight; /** * 修改图片描述控件的外观,不需要修改的传nil * * 参数一 字体颜色,默认为白色 * 参数二 字体,默认为13号字体 * 参数三 背景颜色,默认为黑色半透明 */ UIColor *bgColor = [[UIColor blueColor] colorWithAlphaComponent:0.5]; UIFont *font = [UIFont systemFontOfSize:15]; UIColor *textColor = [UIColor greenColor]; [_scrollView setDescribeTextColor:textColor font:font bgColor:bgColor]; [self.view addSubview:_scrollView]; /** * 通过storyboard创建的轮播控件 */ _scrollView1.imageArray = arr2; //设置分页控件指示器的颜色 [_scrollView1 setPageColor:[UIColor blueColor] andCurrentPageColor:[UIColor redColor]]; //设置图片切换的方式 _scrollView1.changeMode = ChangeModeFade; //用block处理图片点击事件 _scrollView1.imageClickBlock = ^(NSInteger index){ NSLog(@"点击了第%ld张图片", index); }; _scrollView1.time = 3; } #pragma mark XRCarouselViewDelegate - (void)scrollView:(WYScrollView *)carouselView clickImageAtIndex:(NSInteger)index { NSLog(@"点击了第%ld张图片", index); } - (IBAction)start:(id)sender { [_scrollView startTimer]; } - (IBAction)stop:(id)sender { [_scrollView stopTimer]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
代码下载:GITHub: https://github.com/Jacke-xu/WYScrollView
以上是关于新版无限轮播视图,2个imageView完成(不依赖任何三方库)的主要内容,如果未能解决你的问题,请参考以下文章