gpt4 book ai didi

swift3 - 像 SnapChat 一样的 UICollectionView 布局?

转载 作者:行者123 更新时间:2023-12-01 07:45:10 25 4
gpt4 key购买 nike

我们如何创建像 SnapChat 的故事一样的 UICollectionViewLayout?

有什么想法吗?

enter image description here

我想要一个没有外部库的解决方案。

最佳答案

基于 a precedent answer适应您的问题:

-(id)initWithSize:(CGSize)size
{
self = [super init];
if (self)
{
_unitSize = CGSizeMake(size.width/2,80);
_cellLayouts = [[NSMutableDictionary alloc] init];
}
return self;
}
-(void)prepareLayout
{
for (NSInteger aSection = 0; aSection < [[self collectionView] numberOfSections]; aSection++)
{

//Create Cells Frames
for (NSInteger aRow = 0; aRow < [[self collectionView] numberOfItemsInSection:aSection]; aRow++)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:aRow inSection:aSection];
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];

NSUInteger i = aRow%3;
NSUInteger j = aRow/3;
CGFloat offsetY = _unitSize.height*2*j;
CGPoint xPoint;
CGFloat height = 0;
BOOL invert = NO;
if (aRow%6 >= 3) //We need to invert Big cell and small cells => xPoint.x
{
invert = YES;
}
switch (i)
{
case 0:
xPoint = CGPointMake((invert?_unitSize.width:0), offsetY);
height = _unitSize.height;
break;
case 1:
xPoint = CGPointMake((invert?_unitSize.width:0), offsetY+_unitSize.height);
height = _unitSize.height;
break;
case 2:
xPoint = CGPointMake((invert?0:_unitSize.width), offsetY);
height = _unitSize.height*2;
break;
default:
break;
}

CGRect frame = CGRectMake(xPoint.x, xPoint.y, _unitSize.width, height);
[attributes setFrame:frame];
[_cellLayouts setObject:attributes forKey:indexPath];
}

}
}

我将 unitSize 的高度设置为 80,但如果需要,您可以使用屏幕的大小,例如 _unitSize = CGSizeMake(size.width/2,size.height/4.); .

渲染: enter image description here

旁注:调整逻辑或进行更改取决于您,单元格框架计算可能不是“最好看的代码段”。

关于swift3 - 像 SnapChat 一样的 UICollectionView 布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43186246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com