無限輪播專題系列之UICollectionView篇

無限輪播專題系列之UICollectionView篇

開題概述

  • 之所以將無限輪播作為專題來講是因為使用的太廣泛,幾乎99%以上的 APP 都有這個功能,而且涉及到的知識點也不少,另外作為個人對技術總結角度來看,很值得把一些想法力求用最簡單的語言記錄下來讓每一個需要的人都能看懂,也方便自己回顧。

開發思路

  • 功能展示:


  • 思維圖如下


  • 思路詳解:

    • 功能劃分:每一個復雜的界面,我們需要將它細化,就向解答代數題一樣,一步一步來,化繁為簡。從我們需要實現的功能看,可拆分為:創建滾動的view(這里用UICollectionView),一個pageControl,一個提示label(可選)
    • 創建UICollectionView:
  // flowLayout
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = self.frame.size;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowLayout];
    collectionView.backgroundColor = [UIColor clearColor];
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.pagingEnabled = YES;
    collectionView.bounces = NO;
    collectionView.dataSource = self;
    collectionView.delegate = self;
    /** 創建 collectionView 的時候就讓滾蛋到100倍位置時防止一開始用戶向右滾動沒有數據*/
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:_imageStringArray.count *100 inSection:0];
    [collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally ];
    [collectionView registerClass:[WLCollectionViewCell class] forCellWithReuseIdentifier:kCollectionCellID];
    [self addSubview:collectionView];
     _collectionView = collectionView;
  • 創建pageControl + 提示label
 UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 37, self.frame.size.width, 37)];
    bgView.backgroundColor = [UIColor grayColor];
    bgView.alpha = 0.8;
    [self addSubview:bgView];
    // UIPageControl
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    CGSize size = [pageControl sizeForNumberOfPages:_imageStringArray.count];
    pageControl.bounds = CGRectMake(0, 0, size.width, size.height);
    pageControl.center = CGPointMake(bgView.frame.size.width/2, bgView.frame.size.height/2);
    pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    pageControl.numberOfPages = _imageStringArray.count;
    [bgView addSubview:pageControl];
    _payControl = pageControl;
    
    // label
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(pageControl.frame) + 50, 0, 100, bgView.frame.size.height)];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:14.0];
    label.textColor = [UIColor orangeColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"這是第0張圖片";
    [bgView addSubview:label];
    _tipLabel = label;

  • 實現UICollectionViewDataSource 方法:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    // 設置為1000倍時為了當向左滾動超出數組個數的時候就自動從0開始,不用擔心會創造這么多對象消耗內存,UICollectionView會自動復用
    return _imageStringArray.count * 1000;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WLCollectionViewCell *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:kCollectionCellID forIndexPath:indexPath];
    NSInteger index = indexPath.row % _imageStringArray.count;
    cell.imageView.image = [UIImage imageNamed:_imageStringArray[index]];
    
    return cell;
    
}

  • 完成上一步基本可以看到滾動的數據圖片了,下面實現UICollectionViewDelegate方法,讓滾動的時候更新pageControl當前顯示的頁數:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    /** 此處注意滾動到一半的時候才讓分頁控制器顯示下一個 */
    CGFloat offsetX = scrollView.contentOffset.x + scrollView.bounds.size.width * 0.5;
    NSUInteger index = (NSUInteger ) (offsetX / scrollView.bounds.size.width) % _imageStringArray.count;
    _payControl.currentPage = index;
    _tipLabel.text = [NSString stringWithFormat:@"這是第%lu張圖片",(unsigned long)index];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    /** 開始拖拽到時候停止定時器的操作 */
    [self removeTimer];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    /** 拖拽完成的時候 開啟定時器 */
    [self addTimer];
}
  • 添加定時器實現無限滾動
- (void)addTimer {
    
    NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(timerHandler) userInfo:nil repeats:YES];
    [NSRunLoop.mainRunLoop addTimer:timer forMode:NSRunLoopCommonModes];
    _timer = timer;
}
- (void)timerHandler {
    
    CGFloat currentoffsetX = _collectionView.contentOffset.x;
    CGFloat offSetX = currentoffsetX + _collectionView.bounds.size.width;
    _tipLabel.text = [NSString stringWithFormat:@"這是第%f張圖片",offSetX];
    _collectionView.contentOffset = CGPointMake(offSetX, 0);
}
- (void)removeTimer {
    [_timer invalidate];
    _timer = nil;
}

  • 滾動超出區域自動到下一個
  collectionView.delegate = self;
    /** 創建 collectionView 的時候就讓滾蛋到100倍位置時防止一開始用戶向右滾動沒有數據*/
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:_imageStringArray.count *100 inSection:0];
    [collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally ];
    
    // 設置為1000倍時為了當向左滾動超出數組個數的時候就自動從0開始,不用擔心會創造這么多對象消耗內存,UICollectionView會自動復用
    return _imageStringArray.count * 1000;

同時獻上源碼,寫的不好請勿噴,能幫到您是我最大的榮幸與動力。下載地址:github鏈接 https://github.com/VinlorJiang/WLRecycleScrollView.git

非常感謝您的閱讀,也歡迎加入QQ群:39232584,為尊重作者勞動果實,轉載請注明出處!

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

推薦閱讀更多精彩內容

  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,200評論 4 61
  • 雖然劉主任一開始就說就是寫寫,了解一下怎么寫的,但昨天她和劉主任討論后劉主任覺得那個方向確實不可能后,她覺得努力白...
    劉姥姥2017閱讀 363評論 0 0
  • 今天,又要早早的去上班了,上班前把女兒叫起床讓她洗漱完畢吃飯,吃完飯我要去上班了,就和女兒說:“媽媽要去上...
    微笑等待明天閱讀 188評論 0 0
  • 7月底,公司發了一些老客戶名單,我打了十幾個電話,約到一個客戶,這個客戶就是昨天簽單的客戶。當時話術是說送一份小禮...
    終端悍匪阿星閱讀 802評論 0 0