[英]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: 步骤如下:
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.