OStack程序员社区-中国程序员成长平台

标题: ios - UICollectionView 使用自定义动画更改布局 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:37
标题: ios - UICollectionView 使用自定义动画更改布局

我有两个 collectionViewLayout,verticalFlowLayout 和 fullScreenHorizo​​ntalFlowLayout。

@implementation VerticalFlowLayout
- (instancetype)init
{
    if (self = [super init]) {
        self.sectionInset = UIEdgeInsetsMake(12, 0, 0, 0);
        self.minimumLineSpacing = 10;
        self.minimumInteritemSpacing = 0;
        self.itemSize = CGSizeMake(CGRectGetWidth([UIScreen mainScreen].bounds), 244);
    }
    return self;
}
@end

@implementation FullScreenFlowLayout
- (instancetype)init
{
    if (self = [super init]) {
        self.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        self.minimumLineSpacing = 10;
        self.minimumInteritemSpacing = 0;
        self.itemSize = CGSizeMake(CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds));
        self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    }
    return self;
}

当我选择一个单元格时,我想使用自定义动画 更改 collectionView 布局。默认动画是一个线性动画,持续时间为 0.33。

    - (void)collectionViewUICollectionView *)collectionView didSelectItemAtIndexPathNSIndexPath *)indexPath
{
    if ([_collectionView.collectionViewLayout isKindOfClass:[FullScreenFlowLayout class]]) {
        VerticalFlowLayout *flowLayout = [VerticalFlowLayout new];
        [_collectionView setCollectionViewLayout:flowLayout animated:YES completion:^(BOOL finished) {

        }];
    }
    else
        [_collectionView setCollectionViewLayout:[FullScreenFlowLayout new] animated:YES];

}

我该怎么做?



Best Answer-推荐答案


只需将 _collectionView.collectionViewFlowLayout = flowLayout 包裹在 UIView 动画 block 中即可。

[UIView animateWithDuration:5.0 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
    self.collectionView.collectionViewLayout = flowLayout;
} completion:^(BOOL finished) {
    if (finished) {
        // Do Something
    }
}];

关于ios - UICollectionView 使用自定义动画更改布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33454151/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4