UICollectionView实现无限轮播

#import "KGNewsController.h"
#import "KGNewsCell.h"
#import "KGNews.h"
#import "MJExtension.h"

// 生成一个字符串
#define NSString(...) [NSString stringWithFormat:__VA_ARGS__]

#define KGCount 100

@interface KGNewsController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
/** 模型数组 */
@property (strong, nonatomic) NSArray *dataSourceArr;
@property (strong, nonatomic) UIPageControl *pageControl;
@property (strong, nonatomic) NSTimer *timer;
@end

@implementation KGNewsController
- (NSArray *)dataSourceArr {
    if (!_dataSourceArr) {
        // 模型数组
        _dataSourceArr = [KGNews objectArrayWithFilename:@"newses.plist"];
    }
    return _dataSourceArr;
}

static NSString *cell_id = @"KGNewsCell";

- (void)viewDidLoad {
    [super viewDidLoad];
   
    // 注册nib里面的cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"KGNewsCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:cell_id];
   
    // 100组中间开始展示图片
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:KGCount / 2] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
   
    // 添加定时器
    [self addTimer];
   
    // 添加pageControll
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.center = CGPointMake(self.view.bounds.size.width / 2, 200);
    pageControl.pageIndicatorTintColor = [UIColor redColor];
    pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
    [self.view addSubview:pageControl];
    pageControl.numberOfPages = 5;
    self.pageControl = pageControl;
}

/**
 *  添加定时器
 */
- (void)addTimer {
    // 1.创建定时器
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
   
    // 2.把定时器添加到mainRunLoop(主线程也会抽空处理NSTimer的事件)(如果不添加到mainRunLoop,用户做其他操作的时候,定时器就会停止工作)
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    self.timer = timer;
}

/**
 *  移除定时器
 */
- (void)removeTimer {
    // 停止定时器
    [self.timer invalidate];
    // 清空定时器
    self.timer = nil;
}

/**
 *  显示下一页(保证使用定时器不会把100组都轮播完)
 */
- (void)nextPage {
    // 1.马上显示回最中间那组的数据
    NSIndexPath *currentIndexPath = [
    self resetIndexPath];
   
    // 2.计算下一个需要展示的位置(每次都在100组中间组开始)
    NSInteger nextItem = currentIndexPath.item + 1;
    NSInteger nextSection = currentIndexPath.section;
    if (nextItem == self.dataSourceArr.count) {
        nextItem = 0;
        nextSection ++;
    }
   
    NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection];
   
    // 3.通过动画滚动到下一个位置
    [self.collectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
   
    // 4.显示页码
    self.pageControl.currentPage = nextItem;
}

/**
 *  重置indexPath
 */
- (NSIndexPath *)resetIndexPath {
    // 1.当前正在展示的位置
    NSIndexPath *currentIndexPath = [[self.collectionView indexPathsForVisibleItems] lastObject];
   
    // 马上显示回最中间那组的数据
    NSIndexPath *currentIndexPathReset = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:KGCount / 2];
    [self.collectionView scrollToItemAtIndexPath:currentIndexPathReset atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    return currentIndexPathReset;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return KGCount;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSourceArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    KGNewsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cell_id forIndexPath:indexPath];
   
    // 传给cell模型
    cell.news = self.dataSourceArr[indexPath.item];
   
    return cell;
}

#pragma mark - 监听collectionView滚动
/**
 *  用户开始拖拽collectionView
 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self removeTimer];
}

/**
 *  当用户停止拖拽(手指离开)
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self addTimer];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    int page = (int)(scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5) % self.dataSourceArr.count;       self.pageControl.currentPage = page;}- (IBAction)testNSTimer:(UIButton *)sender {    for (int i = 0; i < 10; i ++) {        NSLog(@"测试:如果NSTimer不加入mainRunLoop的情况下,点击button,计时器是否还会工作--%d", i);    }}
@end

你可能感兴趣的:(UICollectionView实现无限轮播)