無(wú)限循環(huán)輪播圖
- 在工作的過(guò)程中,很多情況下會(huì)遇到要使用輪播圖,相信大家也遇到過(guò),使用輪播圖的話分兩種情況:
- 不能無(wú)限循環(huán)的輪播圖
- 這種輪播圖很簡(jiǎn)單,只要使用
UIScrollView
就能完成,也不需要什么計(jì)算什么的。設(shè)置好分頁(yè)就差不多了!
- 這種輪播圖很簡(jiǎn)單,只要使用
- 能無(wú)限循環(huán)的輪播圖
- 這種輪播圖比較麻煩,新手估計(jì)很多都不會(huì)!
- 不能無(wú)限循環(huán)的輪播圖
原理
無(wú)限循環(huán)不能每次都創(chuàng)建UIImageView
,就好像不能每次都創(chuàng)建Cell一樣,需要循環(huán)利用,那么這時(shí)候就要去想如何去設(shè)計(jì),如何重復(fù)利用UIImageView
設(shè)計(jì)原理:
- 在UIScrollView中添加三個(gè)UIImageView
- 在給圖片數(shù)組賦值的時(shí)候,設(shè)置pageControl的頁(yè)碼總數(shù)和當(dāng)前頁(yè)為0
- 在layoutSubviews方法中,先設(shè)置UIScrollView的contentSize,然后設(shè)置三個(gè)UIImageView 和 pageControl的frame
- 在自定義的updateContent方法中,需要對(duì)三個(gè)UIImageView中的圖片進(jìn)行更新,這個(gè)方法要在layoutSubviews方法中調(diào)用一次(最少一次)
- 在updateContent方法中,需要去設(shè)置三個(gè)UIImageView的圖片
- for循環(huán)中每次取出一個(gè)子控件(也就是UIImageView)
- 取出當(dāng)前的pageControl的當(dāng)前頁(yè)碼index
- i == 0 index--;
- i == 2 index++;
- index < 0 index = 總頁(yè)數(shù) - 1;
- index >= 總頁(yè)數(shù) index = 0;
- 設(shè)置imageView的tag = index
- 從數(shù)組中取出圖片賦值給UIImageView
- for循環(huán)結(jié)束后,需要設(shè)置偏移量在中間,也就是總是顯示中間那張圖片
- scrollViewDidScroll:方法中,取出中間的那個(gè)UIImageView,然后設(shè)置pageControl的currnetPage
- scrollViewDidEndDecelerating:方法中需要再次調(diào)用updateContent方法
如果有定時(shí)器
- 在設(shè)置圖片數(shù)組的時(shí)候需要開(kāi)啟定時(shí)器
- scrollViewWillBeginDragging:方法中要停止定時(shí)器
- scrollViewDidEndDragging:方法中要開(kāi)啟定時(shí)器
- 定時(shí)器方法中使用setContentOffset: animated:方法將偏移量設(shè)置成兩倍的寬度
- scrollViewDidEndScrollingAnimation:方法中調(diào)用updateContent方法
需要點(diǎn)擊圖片做一些事情
- 創(chuàng)建UIImageView的時(shí)候可以給每一個(gè)加上一個(gè)手勢(shì)
UITapGestureRecognizer
- 在對(duì)應(yīng)的方法中就可以通知代理點(diǎn)擊的哪一個(gè)圖片
- (void)clickImage:(UITapGestureRecognizer *)tapGest { //tapGest.view.tag獲取當(dāng)前點(diǎn)擊的UIImageView的tag NSLog(@"%ld",tapGest.view.tag); }
上面的方法做到了之后就可以寫(xiě)出一個(gè)完美的無(wú)限滾動(dòng)輪播圖了!當(dāng)然了,設(shè)置圖片輪播圖的時(shí)候一般都是從右往左滾動(dòng)的,也可以設(shè)置成從下往上滾動(dòng)的,這個(gè)就需要在設(shè)置偏移量的時(shí)候進(jìn)行判斷,設(shè)置不同方向上的偏移量就可以實(shí)現(xiàn)不同方向上的滾動(dòng)了!!
實(shí)現(xiàn)代碼
//.h文件內(nèi)容
#import <UIKit/UIKit.h>
@interface YWInfiniteScrollView : UIView
/**
* 圖片數(shù)組
*/
@property (strong, nonatomic) NSArray *images;
/**
* 頁(yè)碼,可以自定義設(shè)置選中和非選中的顏色等
*/
@property (weak, nonatomic, readonly) UIPageControl *pageControl;
/**
* 滾動(dòng)方向:YES是垂直方向,NO是水平方向
*/
@property (assign, nonatomic, getter=isScrollDirectionPortrait) BOOL scrollDirectionPortrait;
@end
//.m文件內(nèi)容
#import "YWInfiniteScrollView.h"
#import "UIImageView+WebCache.h"
//UIImageView總數(shù)
static int const ImageViewCount = 3;
#define kScrollViewHeigth self.scrollView.frame.size.height
#define kScrollViewWidth self.scrollView.frame.size.width
#define kBoundsWidth self.bounds.size.width
#define kBoundsHeight self.bounds.size.height
@interface YWInfiniteScrollView() <UIScrollViewDelegate>
@property (weak, nonatomic) UIScrollView *scrollView;
@property (weak, nonatomic) NSTimer *timer;
/** 第一次執(zhí)行更新視圖*/
@property(nonatomic, assign) BOOL isFirst;
@end
@implementation YWInfiniteScrollView
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
// 滾動(dòng)視圖
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.pagingEnabled = YES;
scrollView.bounces = NO;
scrollView.delegate = self;
[self addSubview:scrollView];
self.scrollView = scrollView;
// 圖片控件
for (int i = 0; i<ImageViewCount; i++) {
UIImageView *imageView = [[UIImageView alloc] init];
[scrollView addSubview:imageView];
}
// 頁(yè)碼視圖
UIPageControl *pageControl = [[UIPageControl alloc] init];
[self addSubview:pageControl];
_pageControl = pageControl;
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.scrollView.frame = self.bounds;
if (self.isScrollDirectionPortrait) {
self.scrollView.contentSize = CGSizeMake(0, ImageViewCount * kBoundsHeight);
} else {
self.scrollView.contentSize = CGSizeMake(ImageViewCount * kBoundsWidth, 0);
}
for (int i = 0; i<ImageViewCount; i++) {
UIImageView *imageView = self.scrollView.subviews[i];
if (self.isScrollDirectionPortrait) {
imageView.frame = CGRectMake(0, i * kScrollViewHeigth, kScrollViewWidth, kScrollViewHeigth);
} else {
imageView.frame = CGRectMake(i * kScrollViewWidth, 0, kScrollViewWidth, kScrollViewHeigth);
}
}
//pageControl的frame需要調(diào)整,根據(jù)個(gè)數(shù)的多少動(dòng)態(tài)調(diào)整,這里就不寫(xiě)了
CGFloat pageW = 80;
CGFloat pageH = 20;
CGFloat pageX = kScrollViewWidth - pageW;
CGFloat pageY = kScrollViewHeigth - pageH;
self.pageControl.frame = CGRectMake(pageX, pageY, pageW, pageH);
if(!self.isFirst){
[self updateContent];
self.isFirst = YES;
}
}
- (void)setImages:(NSArray *)images
{
_images = images;
// 設(shè)置頁(yè)碼
self.pageControl.numberOfPages = images.count;
self.pageControl.currentPage = 0;
// 開(kāi)始定時(shí)器
[self startTimer];
}
#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 找出最中間的那個(gè)圖片控件
NSInteger page = 0;
CGFloat minDistance = MAXFLOAT;
for (int i = 0; i<self.scrollView.subviews.count; i++) {
UIImageView *imageView = self.scrollView.subviews[i];
CGFloat distance = 0;
if (self.isScrollDirectionPortrait) {
distance = ABS(imageView.frame.origin.y - scrollView.contentOffset.y);
} else {
distance = ABS(imageView.frame.origin.x - scrollView.contentOffset.x);
}
if (distance < minDistance) {
minDistance = distance;
page = imageView.tag;
}
}
self.pageControl.currentPage = page;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self stopTimer];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
[self startTimer];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self updateContent];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
[self updateContent];
}
#pragma mark - 內(nèi)容更新
- (void)updateContent
{
// 設(shè)置圖片
for (int i = 0; i<self.scrollView.subviews.count; i++) {
UIImageView *imageView = self.scrollView.subviews[i];
NSInteger index = self.pageControl.currentPage;
if (i == 0) {
index--;
} else if (i == 2) {
index++;
}
if (index < 0) {
index = self.pageControl.numberOfPages - 1;
} else if (index >= self.pageControl.numberOfPages) {
index = 0;
}
imageView.tag = index;
//設(shè)置圖片
[imageView sd_setImageWithURL:[NSURL URLWithString:self.images[index]] placeholderImage:[UIImage imageNamed:@"11"]];
}
// 設(shè)置偏移量在中間
if (self.isScrollDirectionPortrait) {
self.scrollView.contentOffset = CGPointMake(0, kScrollViewHeigth);
} else {
self.scrollView.contentOffset = CGPointMake(kScrollViewWidth, 0);
}
}
#pragma mark - 定時(shí)器處理
- (void)startTimer
{
NSTimer *timer = [NSTimer timerWithTimeInterval:10 target:self selector:@selector(next) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
self.timer = timer;
}
- (void)stopTimer
{
[self.timer invalidate];
self.timer = nil;
}
- (void)next
{
if (self.isScrollDirectionPortrait) {
[self.scrollView setContentOffset:CGPointMake(0, 2 * kScrollViewHeigth) animated:YES];
} else {
[self.scrollView setContentOffset:CGPointMake(2 * kScrollViewWidth, 0) animated:YES];
}
}
@end