准备工作:利用cocopods导入SDWebImage
1.创建一个CYView继承UIView
自动时间
#define kInterval 2
// 图片点击block回调
@property (nonatomic, copy) void(^imageBlock)(NSInteger index);
// 给外界留的接口,只需要传入frame和装有网址的数组
- (instancetype)initWithFrame:(CGRect)frame imageURLs:(NSArray *)imageURLs;
2.导入SD头文件 #import <UIImageView+WebCache.h>
写在类的延展里面
@interface CYView() <UIScrollViewDelegate>
// 最下面的scrollView
@property (nonatomic, strong) UIScrollView *scrollView;
// 网址数组
@property (nonatomic, strong) NSArray *imageURLs;
@property (nonatomic, strong) UIPageControl *pageController;
// 定时器 当用户拖拽时 不需要自动滑动 所以设置为属性
@property (nonatomic, strong) NSTimer *timer;
@end
// 传入frame和装有网址的数组
-(instancetype)initWithFrame:(CGRect)frame imageURLs:(NSArray *)imageURLs {
self = [super initWithFrame:frame];
if (self) {
if (imageURLs.count == 0) {
return nil;
}
self.imageURLs = imageURLs;
[self createScrollView];
[self initImages];
[self createPageController];
// 两张图以上才创建timer
if (self.imageURLs.count >= 2) {
[self createTimer];
}
}
return self;
}
创建分页控制器
- (void)createPageController {
self.pageController = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width/2 - 60, self.frame.size.height - 20, 120, 20)];
self.pageController.pageIndicatorTintColor = [UIColor greenColor];
self.pageController.currentPageIndicatorTintColor = [UIColor redColor];
[self addSubview:self.pageController];
// 一共几个diandian
self.pageController.numberOfPages = self.imageURLs.count;
}
创建滚动视图
- (void)createScrollView {
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.scrollView.contentSize = CGSizeMake(self.frame.size.width * self.imageURLs.count, 0);
self.scrollView.delegate = self;
self.scrollView.backgroundColor = [UIColor yellowColor];
[self addSubview:self.scrollView];
}
初始化图片
- (void)initImages{
// 如果只有一个网址(一张图片)不用循环轮播 直接创建一张imageView
if (self.imageURLs.count == 1) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
[imageView sd_setImageWithURL:[NSURL URLWithString:self.imageURLs[0]]];
[self.scrollView addSubview:imageView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClickFunction)];
// 给每一张图片都添加一个点击事件
[imageView addGestureRecognizer:tap];
// 打开imageView交互
imageView.userInteractionEnabled = YES;
return;
}
// 在左右两边各添加一张imageView
self.scrollView.contentSize = CGSizeMake(self.frame.size.width * (self.imageURLs.count + 2), 0);
// 整夜滚动
self.scrollView.pagingEnabled = YES;
// 一上来就显示第一个
self.scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
for (int i = 0; i < self.imageURLs.count + 2; i++) {
//创建手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClickFunction)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
// 给每一张图片都添加一个点击事件
[imageView addGestureRecognizer:tap];
// 打开imageView交互
imageView.userInteractionEnabled = YES;
// 第一张显示最后一张的内容
if (i == 0) {
[imageView sd_setImageWithURL:[NSURL URLWithString:self.imageURLs.lastObject]];
// 最后一张显示第一张的内容
} else if (i == self.imageURLs.count + 1) {
[imageView sd_setImageWithURL:[NSURL URLWithString:self.imageURLs.firstObject]];
} else {
[imageView sd_setImageWithURL:[NSURL URLWithString:self.imageURLs[i - 1]]];
}
[self.scrollView addSubview:imageView];
}
}
图片点击
- (void)imageClickFunction {
// 回调block
if (self.imageBlock) {
self.imageBlock(self.pageController.currentPage);
}
}
实现滚动协议 #pragma mark ---UIScrollViewDelegate---
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// 如果偏移到第0个 回到最后一个
if (self.scrollView.contentOffset.x <= 0) {
self.scrollView.contentOffset = CGPointMake(self.imageURLs.count * self.frame.size.width, 0);
}
if (self.scrollView.contentOffset.x >= (self.imageURLs.count + 1) * self.frame.size.width) {
self.scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
}
self.pageController.currentPage = self.scrollView.contentOffset.x / self.frame.size.width - 1;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
// 定时器没有提供暂停的方法 我们可以设置他在什么时间开启
[self.timer setFireDate:[NSDate distantFuture]];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
// [self createTimer];
[self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:kInterval]];
}
// 当我们代码改变了偏移量 并且使用了动画的时候 会调用这个方法 不会调用结束减速的方法,可以手动调用
//- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
// [self scrollViewDidEndDecelerating:self.scrollView];
//}
定时器
- (void)createTimer {
self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timeHander) userInfo:nil repeats:YES];
}
// 定时器方法
- (void)timeHander {
CGPoint offSet = self.scrollView.contentOffset;
offSet.x += self.frame.size.width;
[self.scrollView setContentOffset:offSet animated:YES];
}