简体   繁体   English

创建自定义集合视图布局

[英]Creating a custom collection view layout

I am attempting to create a collection view that looks like the schematic below. 我试图创建一个集合视图,如下图所示。 The centre item will always have 'focus' and be larger, its quite a bit like the cover flow concept: 中心项将始终具有“焦点”并更大,它有点像封面流程的概念:

http://s13.postimg.org/n6vzil213/layout_schematic.png http://s13.postimg.org/n6vzil213/layout_schematic.png

I can achieve this with a UICollectionViewFlowLayout, but I want the bottom cell (3) to animate larger and the middle cell (2) to animate smaller as I scroll? 我可以使用UICollectionViewFlowLayout实现此目的,但是我想在滚动时底部单元格(3)为较大的动画设置,而中间单元格(2)为较小的动画设置动画吗? I assume I need to do some math with the scroll view delegate methods. 我假设我需要对滚动视图委托方法进行一些数学运算。

Any idea on how to achieve this? 关于如何实现这一点的任何想法?

You can do this with only subclassing UICollectionViewFlowLayout. 您只能通过子类化UICollectionViewFlowLayout来做到这一点。

Here are the steps: 步骤如下:

  1. Create a UICollectionViewFlowLayout subclass 创建一个UICollectionViewFlowLayout子类
  2. Override shouldInvalidateLayoutForBoundsChange: and return YES 覆盖shouldInvalidateLayoutForBoundsChange:并返回YES
  3. Override layoutAttributesForElementsInRect: and layoutAttributesForItemAtIndexPath:. 覆盖layoutAttributesForElementsInRect:和layoutAttributesForItemAtIndexPath:。 Call the super implementation to get the standard FlowLayout values. 调用超级实现以获取标准FlowLayout值。
  4. Create a method that both the methods from step #3 call that looks changes the layout attributes of the items based on their distance away from the center of the collection view's center. 创建一个方法,该方法将使从步骤3调用的两个方法都根据与集合视图中心中心的距离来更改项目的布局属性。 Use math to set the correct itemSize / transform / 3D transform for the items. 使用数学为项目设置正确的itemSize / transform / 3D转换。

Here's a super rough example that sort of does what you're looking for, without the correct math. 这是一个超级粗糙的示例,没有正确的数学运算就可以完成您要查找的内容。

@implementation MMLayout

-(CGSize)itemSize
{
    return CGSizeMake(200.0f, 200.0f);
}

-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
    return YES;
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSArray *attributes = [super layoutAttributesForElementsInRect:rect];

    for (UICollectionViewLayoutAttributes *atts in attributes) {
        [self scaleForPositionOfAttributes:atts];
    }

    return attributes;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    UICollectionViewLayoutAttributes *atts = [super layoutAttributesForItemAtIndexPath:indexPath];

    [self scaleForPositionOfAttributes:atts];

    return atts;
}

- (void)scaleForPositionOfAttributes:(UICollectionViewLayoutAttributes *)attributes
{
    CGFloat collectionViewHeight = CGRectGetHeight(self.collectionView.bounds);
    CGFloat collectionViewCenter = collectionViewHeight / 2.0f;

    CGFloat scaleForCenter = ABS(collectionViewCenter - (attributes.frame.origin.y - self.collectionView.contentOffset.y)) < 50.0f ? 1.3f : 0.7f;
    attributes.size = CGSizeMake(200.0f * scaleForCenter, 200.0f * scaleForCenter);
}

@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM