iOS UICollectionView 實現輪播圖
阿新 • • 發佈:2018-12-23
利用UICollectionView 實現輪播圖 :
<span style="font-size:24px;">// // ViewController.m // CollectionPhotosView // // Created by 帝炎魔 on 16/5/30. // Copyright © 2016年 帝炎魔. All rights reserved. // /** * UICollectionView 實現輪播圖的實現 將定時器NSTimer 加入到mainrunloop中實現定時輪播 * CollectionCell 和tableViewCell 用法不太一樣, CollectionCell 需要註冊, 告訴系統這種標識對應的cell是什麼型別的cell, 如果緩衝池中沒有, 自動建立這種型別的cell * */ #import "ViewController.h" #import "News.h" #import "YYCell.h" #define kWidth [UIScreen mainScreen].bounds.size.width #define kHeight [UIScreen mainScreen].bounds.size.height #define MaxSections 100 @interface ViewController () <UICollectionViewDelegate, UICollectionViewDataSource> @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) UIPageControl *pageControl; @property (nonatomic, strong) NSMutableArray *news; @property (nonatomic, strong) NSTimer *timer; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.collectionView]; [self.view addSubview:self.pageControl]; // 註冊cell [self.collectionView registerClass:[YYCell class] forCellWithReuseIdentifier:@"Cell"]; // 新增定時器 實現輪播功能呢 [self addTimer]; // Do any additional setup after loading the view, typically from a nib. } - (void)addTimer { _timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:NULL repeats:YES]; } // 定時器的內容 - (void)nextPage { // 獲取當前的 indexPath NSIndexPath *currentIndexPath = [[self.collectionView indexPathsForVisibleItems] lastObject]; NSIndexPath *currentIndexPathSet = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:MaxSections / 2]; [self.collectionView scrollToItemAtIndexPath:currentIndexPathSet atScrollPosition:UICollectionViewScrollPositionLeft animated:NO]; // 設定下一個滾動的item的indexPath NSInteger nextItem = currentIndexPathSet.item + 1; NSInteger nextSection = currentIndexPathSet.section; if (nextItem == self.news.count) { // 當item等於輪播圖的總個數的時候 // item等於0, 分割槽加1 // 未達到的時候永遠在50分割槽中 nextItem = 0; nextSection ++; } // NSLog(@"----%ld---%ld", nextItem, nextSection); NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection]; [self.collectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES]; } #pragma mark ----ScrollView 代理方法 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { // 新增定時器 [self addTimer]; } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { // 移除定時器 [self.timer invalidate]; self.timer = nil; } -(void)scrollViewDidScroll:(UIScrollView *)scrollView { // 滾動時 動態設定 pageControl.currentPage int page = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5) % self.news.count; self.pageControl.currentPage = page; } #pragma mark ---- 建立集合檢視 // 建立集合檢視 - (UICollectionView *)collectionView { if (!_collectionView) { // 建立UICollectionViewFlowLayout約束物件 UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; // 設定item的Size大小 flowLayout.itemSize = CGSizeMake(kWidth, kHeight); // 設定uicollection 的 橫向滑動 flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; flowLayout.minimumLineSpacing = 0; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:flowLayout]; // 設定代理 _collectionView.delegate = self; _collectionView.dataSource = self; // 設定不展示滑動條 _collectionView.showsHorizontalScrollIndicator = NO; // 設定整頁滑動 _collectionView.pagingEnabled = YES; _collectionView.backgroundColor = [UIColor clearColor]; // 設定當前collectionView 到哪個位置(indexPath row 0 section 取中間(50個)) [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:MaxSections / 2] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES]; } return _collectionView; } - (UIPageControl *)pageControl { if (!_pageControl) { UIPageControl *pageControl = [[UIPageControl alloc] init]; pageControl.center = CGPointMake(kWidth / 2, kHeight - 100); pageControl.numberOfPages = _news.count; pageControl.bounds = CGRectMake(0, 0, 150, 40); pageControl.enabled = NO; pageControl.pageIndicatorTintColor = [UIColor blueColor]; pageControl.currentPageIndicatorTintColor = [UIColor redColor]; [self.view addSubview:pageControl]; _pageControl = pageControl; } return _pageControl; } #pragma mark --- 資料來源 - (NSMutableArray *)news { if (!_news) { NSString *path = [[NSBundle mainBundle] pathForResource:@"resource.plist" ofType:nil]; NSArray *arr = [NSArray arrayWithContentsOfFile:path]; _news = [NSMutableArray array]; for (NSDictionary *dic in arr) { [_news addObject:[News newsWithDict:dic]]; } } return _news; } #pragma mark --- 實現collectionView代理方法 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return MaxSections; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.news.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString *string = @"Cell"; YYCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:string forIndexPath:indexPath]; if (!cell) { cell = [[YYCell alloc] init]; } cell.news = self.news[indexPath.row]; return cell; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end </span>