UICollectionView使用自定义动画更改布局

时间:2015-10-31 16:56:52

标签: ios objective-c animation uicollectionview uicollectionviewlayout

我有两个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];

}

我该怎么做?

1 个答案:

答案 0 :(得分:3)

只需将_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
    }
}];