#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实现无限轮播的更多相关文章

  1. 用UICollectionView实现无限轮播图

    用UICollectionView实现无限轮播图 效果 源码 https://github.com/YouXianMing/Animations 细节

  2. iOS开发之三个Button实现图片无限轮播(参考手机淘宝,Swift版)

    这两天使用Reveal工具查看"手机淘宝"App的UI层次时,发现其图片轮播使用了三个UIButton的复用来实现的图片循环无缝滚动.于是乎就有了今天这篇博客,看到“手机淘宝”这个 ...

  3. iOS:实现图片的无限轮播(二)---之使用第三方库SDCycleScrollView

    iOS:实现图片的无限轮播(二)---之使用第三方库SDCycleScrollView 时间:2016-01-19 19:13:43      阅读:630      评论:0      收藏:0   ...

  4. iOS开发UI篇—无限轮播(循环利用)

    iOS开发UI篇—无限轮播(循环利用) 一.无限轮播  1.简单说明 在开发中常需要对广告或者是一些图片进行自动的轮播,也就是所谓的无限滚动. 在开发的时候,我们通常的做法是使用一个UIScrollV ...

  5. iOS开发UI篇—无限轮播(新闻数据展示)

    iOS开发UI篇—无限轮播(新闻数据展示) 一.实现效果        二.实现步骤 1.前期准备 (1)导入数据转模型的第三方框架MJExtension (2)向项目中添加保存有“新闻”数据的pli ...

  6. iOS开发UI篇—无限轮播(循环展示)

    iOS开发UI篇—无限轮播(循环展示) 一.简单说明 之前的程序还存在一个问题,那就是不能循环展示,因为plist文件中只有五个数组,因此第一个和最后一个之后就没有了,下面介绍处理这种循环展示问题的小 ...

  7. iOS开发UI篇—无限轮播(功能完善)

    iOS开发UI篇—无限轮播(功能完善) 一.自动滚动 添加并设置一个定时器,每个2.0秒,就跳转到下一条. 获取当前正在展示的位置. [self addNSTimer]; } -(void)addNS ...

  8. Swift应用案例 1.无限轮播

      从今天开始,我学习的重点开始转向Swift,并且会分享一些自己学习的心得体会,今天给大家带来的的是无限轮播.广告页的无限轮播是非常常见的一个功能,大多数APP都有,大多数程序员也都实现过,今天我们 ...

  9. iOS 两种不同的图片无限轮播

    代码地址如下:http://www.demodashi.com/demo/11608.html 前记 其实想写这个关于无限轮播的记录已经很久很久了,只是没什么时间,这只是一个借口,正如:时间就像海绵, ...

随机推荐

  1. 18.使用for循环计算+1-3+5-7+9-11+13...99的结果

    j = 1 # -1 num1 = 0 #1-3 for i in range(1,100,2): num1 += j * i # -3 j = j * -1 # 1 print(num1) sum1 ...

  2. python爬虫——web前端基础(3)

    超链接的使用------>>>> 链接的引用使用的是<a>标记. <a>标记的基本语法:<a href="链接地址"   ta ...

  3. SP14932 LCA - Lowest Common Ancestor

    Description: 一棵树是一个简单无向图,图中任意两个节点仅被一条边连接,所有连通无环无向图都是一棵树.\(-Wikipedia\) 最近公共祖先(\(LCA\))是--(此处省去对\(LCA ...

  4. JavaScript进阶 - 第5章 小程序,大作用(函数)

    5-1什么是函数 函数的作用,可以写一次代码,然后反复地重用这个代码. 如:我们要完成多组数和的功能. var sum;   sum = 3+2; alert(sum);   sum=7+8 ; al ...

  5. Django框架之MVT(2)

    Django框架之MVT 1.        MVT模型 -     module:模型,和数据库相关的 -     template:模板,存放html文件,模板语法(目的是将变量如果巧妙的嵌入到h ...

  6. 解读 iostat -mxd 1

    #### for AWR 报告 : 建议如下: 不能随便调整db_file_multiblock_read_count 值, 取同样时间段的AWR 报告 02:00 ~ 05:00,所以db_file ...

  7. 去掉 Ctrl + A 全选

    import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public ...

  8. 有关JSOUP学习分享(一)

    其实现在用JSOUP爬虫的也不多了,但是由于最近换公司,做数据爬虫需要用到,就看了下,感觉还是挺好用的,原理什么的感觉和weblogic也差不到哪里去,废话少说,这里就简单的分享下最近接触的干货. J ...

  9. Kendo MVVM (二) ObservableObject 对象

    概述 Kendo MVVM 框架关键的一个部分为 ViewModel,它主要是通过 kendo.data.ObserableObject 来提供支持的.它可以监控改变( UI 变化或是值的变化)并通知 ...

  10. Linux生产服务器常规分区方案

    常规分区方案 / 剩余硬盘大小 swap 100M /boot 100M DB及存储:有大量重要的数据 /data/ 剩余硬盘大小 / 50-200GB swap 1.5倍 /boot 100MB 门 ...