iOS UICollectionViewCells带交叉点的布局
我尝试使用单元格制作iOS UICollectionViewCells带交叉点的布局,ios,objective-c,uicollectionview,uicollectionviewlayout,Ios,Objective C,Uicollectionview,Uicollectionviewlayout,我尝试使用单元格制作UICollectionView,这些单元格在屏幕截图中相交并部分重叠: 此布局是通过设置 self.minimumLineSpacing = -100; 在myUICollectionViewFlowLayout子类中。 当我向下滚动时,一切正常。我知道我想要什么。但当我向上滚动时,我看到另一种行为,与我预期的不同: 所以我的问题是:如何使我的布局在第一个屏幕上看起来与滚动视图方向无关。 注意:我必须同时支持iOS 6和iOS 7 非常感谢您的建议和帮助。嗯,很有意思。由
UICollectionView
,这些单元格在屏幕截图中相交并部分重叠:
此布局是通过设置
self.minimumLineSpacing = -100;
在myUICollectionViewFlowLayout
子类中。
当我向下滚动时,一切正常。我知道我想要什么。但当我向上滚动时,我看到另一种行为,与我预期的不同:
所以我的问题是:如何使我的布局在第一个屏幕上看起来与滚动视图方向无关。
注意:我必须同时支持iOS 6和iOS 7
非常感谢您的建议和帮助。嗯,很有意思。由于集合视图回收单元格,因此当单元格在屏幕上移动或离开屏幕时,它们会不断添加到视图层次结构中或从视图层次结构中删除。这就是说,这是有道理的,当它们被重新添加到视图中时,它们只是作为子视图添加,这意味着当一个单元格被回收时,它现在拥有所有单元格中最高的z索引 纠正这一点的一个相当轻松的方法是手动调整每个单元格的z位置,使其随着索引路径逐渐升高。这样,较低的(y)单元格将始终显示在(z)单元格的上方(y)单元格的上方
找到了解决这个问题的另一个解决方案。我们需要使用
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
希望这能帮助别人。太好了!谢谢。看来,它能按我的要求工作!经过详细的测试,我发现这段代码正确地显示了单元格重叠,但实际上这并没有改变顺序。滚动后,有时它会选择另一个单元格下的单元格。。。
@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