iOS UICollectionViewCells与交叉点的布局

我尝试用单元格制作UICollectionView ,它们在屏幕截图中完成相交和部分重叠。 布局1 这个布局是通过设置达到的

 self.minimumLineSpacing = -100; 

在我的UICollectionViewFlowLayout子类。 当我向下滚动时,一切正常。 我明白我想要什么 但是当我向上滚动时,我看到另一种行为,不像我预期的那样: 布局2 所以我的问题是:无论滚动视图的方向,我怎样才能让我的布局看起来像第一个屏幕。 注意 :我必须同时支持iOS 6和7。

非常感谢任何build议和任何帮助。

嗯,有趣。 由于收集视图可以循环使用单元格,因此它们将在视图层次结构中不断添加或删除。 这就是说,它是有原因的,当它们被重新添加到视图中时,它们被简单地添加为子视图,这意味着当一个单元被回收时,它现在具有所有单元中最高的z-索引。

一个相当无痛的方法来纠正这个问题,就是手动调整每个单元的z位置,使其随着索引path递增。 这样,低(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; } 

find另一种解决scheme来解决这个问题。 我们需要使用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 

希望能帮助别人。