gpt4 book ai didi

ios - UICollectionViewCell 在滚动时用动画移除空间

转载 作者:行者123 更新时间:2023-11-28 08:35:54 28 4
gpt4 key购买 nike

我对 UICollectionViewCell 设计布局有疑问。我需要在 UICollectionView Cell 之间滚动时用动画删除空间,我已经尝试了一些 UICollectionView 的委托(delegate)方法,比如 -

  • CollectionView - minimumLineSpacingForSectionAtIndex
  • CollectionView - minimumInteritemSpacingForSectionAtIndex
  • UIEdgeInsetsMake(0,0,0,0);
  • scrollViewDidEndDragging

但达不到标准。这是代码:

  - (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
float cellWidth = screenWidth / 2.1; //Replace the divisor with the column count requirement. Make sure to have it in float.
CGSize size = CGSizeMake(cellWidth, cellWidth);

return size;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 1.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 1.0;
}

// Layout: Set Edges
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0,0,0,0); // top, left, bottom, right
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
UIEdgeInsetsMake(0,0,0,0); // after scroll cell space not remove.
}

enter image description here

最佳答案

我不完全明白你想做什么,但如果你想改变 Collection View 的布局属性并动画改变,最好的方法是调用 setLayout:animated: .

像这样——

UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = 1.0;
layout.minimumInteritemSpacing = 1.0;
layout.itemSize = CGSizeMake(cellWidth, cellHeight);

UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout]

然后每当你想改变它时,只需调用 setLayout:animated: 方法:

UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout alloc] init];
initialLayout.minimumLineSpacing = 5.0;
initialLayout.minimumInteritemSpacing = 7.0;
layout.itemSize = CGSizeMake(newCellWidth, newCellHeight);

[collectionView setLayout:layout animated:YES];

关于ios - UICollectionViewCell 在滚动时用动画移除空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37718732/

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