21

我正在尝试创建一种效果,在更改框架大小的同时更改 UICollectionView 的布局

最初,collectionView 布局呈现“缩略图”画廊风格全屏。

将框架调整为细条后 - 我想呈现“胶片条”样式布局

两种布局都可以按预期独立工作。

我尝试了类似这样的代码:

[UIView animateWithDuration:1
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         self.collectionview.frame = newFrame; // animate the frame size


                     }
                     completion:^(BOOL finished) {
                        [self.collectionView.collectionViewLayout invalidateLayout];

    [self.collectionView setCollectionViewLayout:filmstriplayout animated:YES];    // now set the new layout
                     }];

但它看起来非常不稳定,并且没有按预期调整大小。

有没有一种方法可以在为更改设置动画的同时同时更改 collectionview 布局和框架大小?

4

3 回答 3

21

我没有具体的答案,但有一些建议可以考虑。

UICollectionView并不总是优雅地处理切换布局实例。这是对问题和一些解决方法的很好的讨论

但实际上我在实践中所做的对我有用的是在一个布局对象中实现这两种布局,该对象知道如何在布局模式之间切换。我发现在批量更新块中切换布局模式比使用setCollectionViewLayout两个不同的布局实例问题更少:

[self.collectionView performBatchUpdates:^{
    MyLayout *layout = (MyLayout *)self.collectionView.collectionViewLayout;
    layout.mode = otherLayoutMode;
} completion:nil];
于 2013-09-08T22:12:13.273 回答
20

首先设置网格项大小,gridItemSize = CGSizeMake(98, 98);然后执行 UICollectionView 的批处理操作。集合视图中的项目通过动画改变它们的大小。:)

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout*)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
   return CGSizeMake(gridItemSize.width, gridItemSize.height);
}

[self.collectionview performBatchUpdates:^{
    [self.collectionview.collectionViewLayout invalidateLayout];
    [self.collectionview setCollectionViewLayout:self.collectionview.collectionViewLayout animated:YES];
} completion:^(BOOL finished) {    
}];
于 2013-11-04T07:27:56.087 回答
-3

每当您更新 CollectionView 时,这是一个简单的技巧:

[self.collectionView removeFromSuperview]; //First remove the Collection View

//Relocate the view with layouts
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
layout.minimumInteritemSpacing = 10;
layout.minimumLineSpacing = 10;

 self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.width,100) collectionViewLayout:layout];
[self.collectionView setDataSource:self];
[self.collectionView setDelegate:self];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[self.collectionView reloadData];

[self.view addSubview:self.collectionView];
于 2016-02-22T07:08:52.880 回答