2013-12-19 17 views
3

我儘量讓UICollectionView與細胞,交叉和部分地相互覆蓋,因爲它是在截圖完成: Layout 1 這種佈局是由我UICollectionViewFlowLayout子類設置的iOS UICollectionViewCells佈局與路口

self.minimumLineSpacing = -100; 

達到。 向下滾動時,一切正常。我明白我想要的。但是當我向上滾動時,我看到另一種行爲,不像我預期的那樣: Layout 2 所以我的問題是:無論滾動視圖方向如何讓我的佈局看起來像第一個屏幕一樣。 注意:我必須同時支持iOS 6和7.

非常感謝任何建議和任何幫助。

回答

1

發現另一種溶劑來解決這個問題。我們需要使用UICollectionViewFlowLayout子類。

@interface MyFlowLayout : UICollectionViewFlowLayout 
@end 

@implementation MyFlowLayout 

- (void)prepareLayout { 
    [super prepareLayout]; 
    // This allows us to make intersection and overlapping 
    self.minimumLineSpacing = -100; 
} 

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect 
{ 
    NSArray *layoutAttributes = [super layoutAttributesForElementsInRect:rect]; 
    for (UICollectionViewLayoutAttributes *currentLayoutAttributes in layoutAttributes) { 
     // Change zIndex allows us to change not only visible position, but logic too 
     currentLayoutAttributes.zIndex = currentLayoutAttributes.indexPath.row; 
    } 
    return layoutAttributes; 
} 

@end 

希望能幫助別人。

2

嗯,有趣。由於集合視圖可以循環使用單元格,因此它們會在視圖層次結構中移動或移出屏幕時不斷添加和移除。這就是說,它有理由,當它們被重新添加到視圖中時,它們只是作爲子視圖添加,這意味着當一個單元格被回收時,它現在具有所有單元格中最高的z-index。

解決此問題的一個相當無痛的方法是手動調整每個單元的z位置,使其隨着索引路徑遞增。這樣,低(y)的細胞總是會出現在(z)上方的細胞(y)之上。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellID = @"CELLID"; 

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; 

    if (cell.layer.zPosition != indexPath.row) { 
     [cell.layer setZPosition:indexPath.row]; 
    } 

    return cell; 
} 
+0

完美!非常感謝。看來,它可以按我的需要工作! –

+0

@AccidBright很高興聽到它幫助! –

+0

經過詳細的測試後,我發現這段代碼能正確顯示單元重疊,但實際上這並不會改變順序。滾動後有時會選擇另一個下的單元格... –