我尝试使用库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
的调用返回为零,但我不明白为什么。我的猜测是它与视图控制器不存在有关...?
答案 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];
}
根据您的代码段,我看到两件事:
toCollectionView
transitionLayoutForOldLayout
方法未返回TLTransitionLayout