collectionView中仅仅有三个cell 每次显示的都是第二个cellide
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {spa
CycleViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];it
//indexPath.item - 1 至关于index左移加1 右移减一io
//indexPath.item - 1 假设左移就至关于要显示第三个cell 2-1 ,至关于self.currentIndex + 1class
//indexPath.item - 1 假设右移就至关于要显示第一个cell 0-1 。至关于self.currentIndex - 1scroll
NSInteger index = (self.currentIndex + indexPath.item - 1 + self.imageURLs.count) % self.imageURLs.count;queue
cell.imageURL = self.imageURLs[index];方法
return cell;im
}animate
// 在滚动视图全然中止滚动后会调用的方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
// 1. 依据contentOffset可以推断出停留住的页面
int page = scrollView.contentOffset.x / scrollView.bounds.size.width;
NSLog(@"第 %d 页", page);
// 2. 假设是第0页,self.currentIndex - 1,假设是第2页,self.currentIndex +1;
self.currentIndex = (self.currentIndex + page - 1 + self.imageURLs.count) % self.imageURLs.count;
// 3. 让collection滚动会第一个页面
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:1 inSection:0];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}