1.因为UICollectionView继承于UIScrollView,所以我们可以使用UICollectionView来实现图片的无限轮播,当图片数量增加UICollectionView可以帮助我们减少内存的消耗。
#import "ViewController.h"
static NSString *cellIdef_ = @"cellIdef";
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) UIPageControl *pageControl;
@property (nonatomic,strong) NSTimer *timer;
- (void)initialUSerInterface;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self initialUSerInterface];
// Do any additional setup after loading the view, typically from a nib.
}
//懒加载pageControl
- (UIPageControl *)pageControl{
if (_pageControl == nil) {
//分页控件,本质上和scorllView没有任何关系,是2个独立的控件
_pageControl = [[UIPageControl alloc]init];
_pageControl.numberOfPages = 5;
CGSize size = [_pageControl sizeForNumberOfPages:5];
_pageControl.bounds = CGRectMake(0, 0, size.width, size.width);
_pageControl.center = CGPointMake(self.view.center.x, 130);
_pageControl.pageIndicatorTintColor = [UIColor redColor];
[self.view addSubview:_pageControl];
}
return _pageControl;
}
- (void)initialUSerInterface{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
layout.itemSize = CGSizeMake(self.view.frame.size.width, 250);
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 10, self.view.frame.size.width, 250) collectionViewLayout:layout];
collectionView.backgroundColor = [UIColor whiteColor];
collectionView.delegate = self;
collectionView.dataSource = self;
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellIdef_];
collectionView.pagingEnabled = YES;
collectionView.showsHorizontalScrollIndicator = NO;
collectionView.showsVerticalScrollIndicator = NO;
collectionView.bounces = NO;
collectionView.contentSize = CGSizeMake(5 * collectionView.bounds.size.width, 0);
[self.view addSubview:collectionView];
self.collectionView = collectionView;
[self addNSTime];
self.pageControl.currentPage = 0;
}
//添加定时器
- (void)addNSTime{
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
//添加到runloop中
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
[timer fire];
self.timer = timer;
}
//删除定时器
- (void)removeNSTimer{
[self.timer invalidate];
self.timer = nil;
}
//自动滚动
- (void)nextPage{
NSInteger currentNumber = self.pageControl.currentPage;
CGFloat x = ((currentNumber + 1)%5) * self.collectionView.bounds.size.width;
if (currentNumber <= 5) {
[self.collectionView setContentOffset:CGPointMake(x, 0) animated:YES];
}else{
[self.collectionView setContentOffset:CGPointMake(x, 0) animated:NO];
}
self.pageControl.currentPage = x;
}
#pragma mark --)<UICollectionViewDataSource,UICollectionViewDelegate>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 5;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdef_ forIndexPath:indexPath];
cell.backgroundColor = [UIColor yellowColor];
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%ld",indexPath.row + 1]]];
image.frame = cell.bounds;
[cell addSubview:image];
return cell;
}
//当用户开始拖拽的时候就调用
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
[self removeNSTimer];
}
//当用户停止拖拽的时候调用
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[self addNSTime];
}
//设置页码
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
int page = (int)(scrollView.contentOffset.x/scrollView.frame.size.width + 0.5)%5;
self.pageControl.currentPage = page;
}