// 最下面的scrollView
@property (nonatomic, strong) UIScrollView *scrollView;
// 網址數組
@property (nonatomic, strong) NSArray *imageURLs;
@property (nonatomic, strong) UIPageControl *pageController;
// 定時器 當用戶拖拽時 不需要自動滑動 所以設置為屬性
@property (nonatomic, strong) NSTimer *timer;
@end
@implementation CarouselView
// 傳入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];
// 兩張圖以上才創(chuàng)建timer
if (self.imageURLs.count >= 2) {
[self createTimer];
}
}
return self;
}
- (void)createPageController {
self.pageController = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width - 60, self.frame.size.height - 20, 50, 20)];
[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{
// 如果只有一個網址(一張圖片)不用循環(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)];
// 給每一張圖片都添加一個點擊事件
[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.imageClick) {
self.imageClick(self.pageController.currentPage);
}
}
- (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)createTimer {
self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timeHander) userInfo:nil repeats:YES];
}
- (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)timeHander {
CGPoint offSet = self.scrollView.contentOffset;
offSet.x += self.frame.size.width;
[self.scrollView setContentOffset:offSet animated:YES];
}