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