1. 程式人生 > >使用UICollectionView實現圖片輪播

使用UICollectionView實現圖片輪播

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; }