在工作中有時候我們會遇到無限滾動,比如相冊,廣告等,今天就用UICollectionView來實現無限滾動,話不多說,都在代碼中了!
#define ID @"photo"
#define KMaxSections 100
@interface PhotoViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, weak) IBOutlet UIPageControl *pageContol;
@property (nonatomic, strong) NSArray *photoes;
@property (nonatomic, strong) NSTimer *timer;
@end
@implementation PhotoViewController
- (NSArray *)photos
{
if (_photoes == nil) {
NSMutableArray * tmpArray = [NSMutableArray array];
for (NSInteger i = 0; i < 20; i ++) {
UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg",i + 1]];
[tmpArray addObject:image];
}
_photoes = tmpArray;
}
return _photoes;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 註冊cell
[self.collectionView registerNib:[UINib nibWithNibName:@"photo" bundle:nil] forCellWithReuseIdentifier:ID];
// 默認顯示最中間的那組
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:KMaxSections/2] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
// 添加定時器
[self addTimer];
}
/**
* 添加定時器
*/
- (void)addTimer
{
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
self.timer = timer;
}
/**
* 移除定時器
*/
- (void)removeTimer
{
// 停止定時器
[self.timer invalidate];
self.timer = nil;
}
- (NSIndexPath *)resetIndexPath
{
// 當前正在展示的位置
NSIndexPath *currentIndexPath = [[self.collectionView indexPathsForVisibleItems] lastObject];
// 馬上顯示回最中間那組的數據
NSIndexPath *currentIndexPathReset = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:KMaxSections/2];
[self.collectionView scrollToItemAtIndexPath:currentIndexPathReset atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
return currentIndexPathReset;
}
/**
* 下一頁
*/
- (void)nextPage
{
// 1.馬上顯示回最中間那組的數據
NSIndexPath *currentIndexPathReset = [self resetIndexPath];
// 2.計算出下一個需要展示的位置
NSInteger nextItem = currentIndexPathReset.item + 1;
NSInteger nextSection = currentIndexPathReset.section;
if (nextItem == self.photoes.count) {
nextItem = 0;
nextSection++;
}
NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection];
// 3.通過動畫滾動到下一個位置
[self.collectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.photoes.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return KMaxSections;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
cell.photo = self.photoes[indexPath.item];
return cell;
}
#pragma mark - UICollectionViewDelegate
/**
* 當用戶即將開始拖拽的時候就調用
*/
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self removeTimer];
}
/**
* 當用戶停止拖拽的時候就調用
*/
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
[self addTimer];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
int page = (int)(scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5) % self.newses.count;
self.pageContol.currentPage = page;
}