【问题标题】:UICollectionView change layout with custom animationUICollectionView 使用自定义动画更改布局
【发布时间】:2016-01-31 22:49:38
【问题描述】:

我有两个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)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)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];

}

我该怎么做?

【问题讨论】:

    标签: ios objective-c animation uicollectionview uicollectionviewlayout


    【解决方案1】:

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

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

    【讨论】:

    • 这里是自定义collectionView布局动画的完整指南:github.com/cnoon/CollectionViewAnimations
    • 我认为你从询问如何做到这一点到创建一个有据可查的 GitHub 存储库供其他人参考真是太棒了。干得好。
    猜你喜欢
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多