2013-04-27 202 views
4

我在我的iOS應用中實現了UICollectionView。我擁有它,因此每個單元格都是屏幕的寬度,我想在水平滾動時鎖定可見單元格並將其移動到屏幕中心。下面的代碼我只適用於第一個單元格。我不明白如何讓它適用於用戶可見的任何單元格。UICollectionView水平滾動

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ 
    int index = 0; 
    for (TextCell * t in [_tabBarCollectionView visibleCells]) { 
     if ((t.center.x>0)&&(t.center.x<[[UIScreen mainScreen]bounds].size.width)) { 
      [_tabBarCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES]; 
      break; 
     } 
     index++; 
    } 

} 

回答

4

您可以打開收集視圖的分頁,它會產生這種效果。轉到已添加集合的xib文件或故事板,並在其屬性下啓用分頁。

+0

好,但我該如何檢測哪些索引是可見的? – BDGapps 2013-04-27 19:58:39

+0

在滾動視圖中調用此方法結束減速,如果您有分頁並且您說單元格是屏幕的大小,則只會返回一個位置: - (NSArray *)indexPathsForVisibleItems 編輯:so do [_tabBarCollectionView indexPathsForVisibleItems]; – 2013-04-27 20:08:05

+0

好,但我有2個collectionViews,所以我如何檢測它正在談論哪個集合視圖 – BDGapps 2013-04-27 20:24:10