UICollectionViewController使用TLTransitionLayout进行转换

时间:2014-12-11 13:39:04

标签: ios objective-c uicollectionview

我尝试使用库TLTransitionLayout在两个UICollectionViewController之间进行转换。

我可以毫无问题地转换第一个视图控制器的布局。但是尝试对第二个应用程序执行相同操作会使应用程序崩溃。

这是我的代码:

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{

    UIView *inView = [transitionContext containerView];
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIView *fromView = [fromVC view];
    UIViewController *toVC  = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *toView = toVC.view;

    self.fromCollectionView = ((UICollectionViewController *)fromVC).collectionView;
    self.toCollectionView = ((UICollectionViewController *)toVC).collectionView;
    self.fromCollectionView.delegate = self;

    //[self.toCollectionView transitionToCollectionViewLayout:self.toCollectionView.collectionViewLayout
    [self.fromCollectionView transitionToCollectionViewLayout:self.toCollectionView.collectionViewLayout
                                                   duration:[self transitionDuration:transitionContext]
                                                     easing:CubicEaseOut
                                                 completion:^(BOOL completed, BOOL finished) {
                                                     [inView insertSubview:toView aboveSubview:fromView];
                                                     [transitionContext completeTransition:YES];
                                                 }];
}



- (UICollectionViewTransitionLayout *)collectionView:(UICollectionView *)collectionView transitionLayoutForOldLayout:(UICollectionViewLayout *)fromLayout newLayout:(UICollectionViewLayout *)toLayout
{
    UICollectionViewTransitionLayout *layout = [[UICollectionViewTransitionLayout alloc] initWithCurrentLayout:fromLayout nextLayout:toLayout];
    return layout;
}

实际上,它有效。如果我将开始转换的行更改为toCollectionView而不是fromCollectionView,则会因错误而崩溃:

  

CRASH:*** setObjectForKey:对象不能为nil(键:   transitionLayout)

似乎来自startInteractiveTransitionToCollectionViewLayout库内的TLTransitionLayout的调用返回为零,但我不明白为什么。我的猜测是它与视图控制器不存在有关...?

1 个答案:

答案 0 :(得分:0)

您的集合视图需要有一个委托,它返回TLTransitionLayout的实例,如下所示:

- (UICollectionViewTransitionLayout *)collectionView:(UICollectionView *)collectionView transitionLayoutForOldLayout:(UICollectionViewLayout *)fromLayout newLayout:(UICollectionViewLayout *)toLayout
{
    NSArray *supplementaryKinds = ...; // optional supplementary view kinds
    return [[TLTransitionLayout alloc] initWithCurrentLayout:fromLayout nextLayout:toLayout supplementaryKinds:supplementaryKinds];
}

根据您的代码段,我看到两件事:

  1. 您似乎没有在toCollectionView
  2. 上设置代理
  3. 您的transitionLayoutForOldLayout方法未返回TLTransitionLayout
  4. 的实例