準(zhǔn)備工作:利用cocopods導(dǎo)入SDWebImage
1.創(chuàng)建一個(gè)CYView繼承UIView
自動(dòng)時(shí)間
#define kInterval 2
// 圖片點(diǎn)擊block回調(diào)
@property (nonatomic, copy) void(^imageBlock)(NSInteger index);
// 給外界留的接口匹层,只需要傳入frame和裝有網(wǎng)址的數(shù)組
- (instancetype)initWithFrame:(CGRect)frame imageURLs:(NSArray *)imageURLs;
2.導(dǎo)入SD頭文件 #import <UIImageView+WebCache.h>
寫在類的延展里面
@interface CYView() <UIScrollViewDelegate>
// 最下面的scrollView
@property (nonatomic, strong) UIScrollView *scrollView;
// 網(wǎng)址數(shù)組
@property (nonatomic, strong) NSArray *imageURLs;
@property (nonatomic, strong) UIPageControl *pageController;
// 定時(shí)器 當(dāng)用戶拖拽時(shí) 不需要自動(dòng)滑動(dòng) 所以設(shè)置為屬性
@property (nonatomic, strong) NSTimer *timer;
@end
// 傳入frame和裝有網(wǎng)址的數(shù)組
-(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];
// 兩張圖以上才創(chuàng)建timer
if (self.imageURLs.count >= 2) {
[self createTimer];
}
}
return self;
}
創(chuàng)建分頁(yè)控制器
- (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];
// 一共幾個(gè)diandian
self.pageController.numberOfPages = self.imageURLs.count;
}
創(chuàng)建滾動(dòng)視圖
- (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{
// 如果只有一個(gè)網(wǎng)址(一張圖片)不用循環(huán)輪播 直接創(chuàng)建一張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)];
// 給每一張圖片都添加一個(gè)點(diǎn)擊事件
[imageView addGestureRecognizer:tap];
// 打開imageView交互
imageView.userInteractionEnabled = YES;
return;
}
// 在左右兩邊各添加一張imageView
self.scrollView.contentSize = CGSizeMake(self.frame.size.width * (self.imageURLs.count + 2), 0);
// 整夜?jié)L動(dòng)
self.scrollView.pagingEnabled = YES;
// 一上來(lái)就顯示第一個(gè)
self.scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
for (int i = 0; i < self.imageURLs.count + 2; i++) {
//創(chuàng)建手勢(shì)
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)];
// 給每一張圖片都添加一個(gè)點(diǎn)擊事件
[imageView addGestureRecognizer:tap];
// 打開imageView交互
imageView.userInteractionEnabled = YES;
// 第一張顯示最后一張的內(nèi)容
if (i == 0) {
[imageView sd_setImageWithURL:[NSURL URLWithString:self.imageURLs.lastObject]];
// 最后一張顯示第一張的內(nèi)容
} 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];
}
}
圖片點(diǎn)擊
- (void)imageClickFunction {
// 回調(diào)block
if (self.imageBlock) {
self.imageBlock(self.pageController.currentPage);
}
}
實(shí)現(xiàn)滾動(dòng)協(xié)議 #pragma mark ---UIScrollViewDelegate---
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// 如果偏移到第0個(gè) 回到最后一個(gè)
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 {
// 定時(shí)器沒有提供暫停的方法 我們可以設(shè)置他在什么時(shí)間開啟
[self.timer setFireDate:[NSDate distantFuture]];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
// [self createTimer];
[self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:kInterval]];
}
// 當(dāng)我們代碼改變了偏移量 并且使用了動(dòng)畫的時(shí)候 會(huì)調(diào)用這個(gè)方法 不會(huì)調(diào)用結(jié)束減速的方法铅忿,可以手動(dòng)調(diào)用
//- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
// [self scrollViewDidEndDecelerating:self.scrollView];
//}
定時(shí)器
- (void)createTimer {
self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timeHander) userInfo:nil repeats:YES];
}
// 定時(shí)器方法
- (void)timeHander {
CGPoint offSet = self.scrollView.contentOffset;
offSet.x += self.frame.size.width;
[self.scrollView setContentOffset:offSet animated:YES];
}