輪播圖和定時(shí)器的核心代碼

// 最下面的scrollView

@property (nonatomic, strong) UIScrollView *scrollView;

// 網(wǎng)址數(shù)組

@property (nonatomic, strong) NSArray *imageURLs;

@property (nonatomic, strong) UIPageControl *pageController;

// 定時(shí)器 當(dāng)用戶拖拽時(shí) 不需要自動滑動 所以設(shè)置為屬性

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation CarouselView

// 傳入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;

}

- (void)createPageController {

self.pageController = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width - 60, self.frame.size.height - 20, 50, 20)];

[self addSubview:self.pageController];

// 一共幾個(gè)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{

// 如果只有一個(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);

// 整 頁滾動

self.scrollView.pagingEnabled = YES;

// 一上來就顯示第一個(gè)

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)];

// 給每一張圖片都添加一個(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.imageClick) {

self.imageClick(self.pageController.currentPage);

}

}

- (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;

}

/**

*? 定時(shí)器方法

*/

- (void)createTimer {

self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timeHander) userInfo:nil repeats:YES];

}

- (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)我們代碼改變了偏移量 并且使用了動畫的時(shí)候? 會調(diào)用這個(gè)方法 不會調(diào)用結(jié)束減速的方法,我可以手動調(diào)用

//- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {

//? ? [self scrollViewDidEndDecelerating:self.scrollView];

//}

// 定時(shí)器方法

- (void)timeHander {

CGPoint offSet = self.scrollView.contentOffset;

offSet.x += self.frame.size.width;

[self.scrollView setContentOffset:offSet animated:YES];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容