alg*_*gal 6 ios uicollectionview
默认情况下(即,具有垂直滚动方向),则通过UICollectionViewFlowLayout处开始勾画出细胞中的左上,去从左至右,直到该行被填充,然后进行到下一行向下。相反,我想它在启动左下,从左向右走,直到该行被填满,然后进行下一行了。
是否可以通过子类化UIScrollViewFlowLayout来实现此目的的简单方法,还是我基本上需要从头开始重新实现该类?
苹果公司的文档上继承流布局建议,我只需要重写和重新实现自己的版本layoutAttributesForElementsInRect:, layoutAttributesForItemAtIndexPath:以及collectionViewContentSize。但这似乎并不简单。由于UICollectionViewFlowLayout不会公开其在内部进行的任何网格布局计算,因此prepareLayout我需要从其为顶部到底部布局生成的值中推断出底部到顶部布局所需的所有布局值。
我不确定这是否可能。尽管我可以重复使用它的计算,即关于哪些项目组放在同一行中,但我将需要计算新的y偏移量。为了进行计算,我将需要有关所有项目的信息,但是那些超类方法不会对此进行报告。
您基本上可以用简单的逻辑来实现它,但这似乎有些奇怪。如果collectionview内容大小与collectionview边界相同或者所有单元格都可见,那么您可以使用简单的flowLayout来实现它,如下所示,
@implementation SimpleFlowLayout
- (UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewLayoutAttributes *attribute = [super layoutAttributesForItemAtIndexPath:indexPath];
[self modifyLayoutAttribute:attribute];
return attribute;
}
- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect{
NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
for(UICollectionViewLayoutAttributes *attribute in attributes){
[self modifyLayoutAttribute:attribute];
}
return attributes;
}
- (void)modifyLayoutAttribute:(UICollectionViewLayoutAttributes*)attribute{
CGSize contentSize = self.collectionViewContentSize;
CGRect frame = attribute.frame;
frame.origin.x = contentSize.width - attribute.frame.origin.x - attribute.frame.size.width;
frame.origin.y = contentSize.height - attribute.frame.origin.y - attribute.frame.size.height;
attribute.frame = frame;
}
@end
Run Code Online (Sandbox Code Playgroud)
所以这个图看起来像这样,

但是,如果您使用的行数超过了屏幕上同时显示的行数,那么重用似乎会出现一些问题。由于 UICollectionView 数据源方法,collectionView:cellForItemAtIndexPath: 线性工作并在用户滚动时询问 indexPath,因此单元格会以通常递增的 indexPath 模式(例如 1 --- 100)询问,尽管我们希望它反转此模式。滚动时,我们需要 collectionView 按降序询问项目,因为我们的 100 个项目位于顶部,1 个项目位于底部。所以,我对如何实现这一点没有任何具体的想法。
| 归档时间: |
|
| 查看次数: |
4923 次 |
| 最近记录: |