配置UICollectionViewFlowLayout以从下到上排列行 [英] Configure UICollectionViewFlowLayout to lay out rows from bottom to top

查看:35
本文介绍了配置UICollectionViewFlowLayout以从下到上排列行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下(即,具有垂直滚动方向),UICollectionViewFlowLayout通过从左上角开始,从左到右,直到行被填充,然后继续进行布局.到的下一行.相反,我希望它从左下开始,从左到右,直到行被填充,然后继续进行下一行 up .

By default (i.e., with a vertical scrolling direction), the UICollectionViewFlowLayout lays out cells by starting at the top-left, going from left to right, until the row is filled, and then proceeds to the next line down. Instead, I would like it to start at the bottom-left, go from left to right, until the row is filled, and then proceed to the next line up.

是否可以通过子类化UIScrollViewFlowLayout来实现此目的的简单方法,还是我基本上需要从头开始重新实现该类?

Is there a straightforward way to do this by subclassing UIScrollViewFlowLayout, or do I basically need to re-implement that class from scratch?

Apple的文档建议,我只需要覆盖并重新实现自己的 layoutAttributesForElementsInRect版本: layoutAttributesForItemAtIndexPath:,以及 collectionViewContentSize .但这似乎并不简单.由于UICollectionViewFlowLayout不会公开其在 prepareLayout 内部进行的任何网格布局计算,因此我需要从其为顶部-顶部生成的值中推断出底部到顶部布局所需的所有布局值.底部布局.

Apple's documentation on subclassing flow layout suggests that I only need to override and re-implement my own version of layoutAttributesForElementsInRect:, layoutAttributesForItemAtIndexPath:, and collectionViewContentSize. But this does not seem straightforward. Since UICollectionViewFlowLayout does not expose any of the grid layout calculations it makes internally in prepareLayout, I need to deduce all the layout values needed for the bottom-to-top layout from the values it generates for a top-to-bottom layout.

我不确定是否可以.虽然我可以重新使用它的计算,即关于哪些项目组放在相同的行上,但我将需要计算新的y偏移量.为了进行计算,我将需要有关所有项目的信息,但是那些超类方法不会报告该信息.

I am not sure this is possible. While I can re-use its calculations about which groups of items get put on the same rows, I will need to calculate new y offsets. And to make my calculations I will need information about all the items, but those superclass methods do not report that.

推荐答案

您基本上可以使用简单的逻辑来实现它,但是这似乎有些奇怪.如果collectionview的contentsize与collectionview的边界相同,或者所有单元格都可见,则可以使用简单的flowLayout来实现此目的,

You could basically implement it with a simple logic, however this seems to be some how odd. If the collectionview contentsize is same as that of the collectionview bounds or if all the cells are visible then you could implement this with simple flowLayout as this,

@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

所以这个数字看起来像这样,

And so the figure looks like this,

但是,如果您使用更多的行,而不是同时显示在屏幕上的行,则重用似乎存在一些问题.由于UICollectionView数据源方法collectionView:cellForItemAtIndexPath:是线性工作的,并且随着用户滚动而要求indexPath,因此以通常递增的indexPath模式(例如1 --- 100)询问单元格,尽管我们希望它反转此模式.滚动时,由于我们的100个项目位于顶部,而1个项目位于底部,因此我们需要collectionView以降序要求我们提供项目.因此,我对如何实现这一目标没有任何特别的想法.

But, if you use more rows, more than the that can be seen on the screen at the same time, then there seems to be some problem with reusing. Since the UICollectionView datasource method, collectionView:cellForItemAtIndexPath: works linearly and asks for the indexPath as the user scrolls, the cell are asked in the usual increasing indexPath pattern such as 1 --- 100 though we would want it to reverse this pattern. While scrolling we would need the collectionView to ask us for the items in decreasing order since our 100 item resides at top and 1 item at bottom. So, I dont have any particular idea about how this could be accomplished.

这篇关于配置UICollectionViewFlowLayout以从下到上排列行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆