自定义转换动画在解除时不调用VC生命周期方法

Dan*_*nny 37 ios ios7

所以我构建了一个自定义呈现过渡动画,除了视图控制器生命周期方法没有被调用时,一切似乎都很好.

在呈现控制器之前,我使用UIModalPresentationCustom样式将呈现VC保留在视图层次结构中,但是一旦我关闭了呈现的VC,就不会在我的呈现控制器上调用viewWillAppear和viewDidAppear.我错过了一个我需要明确调用以获取这些方法的步骤吗?我知道手动调用这些方法不是正确的解决方案.

这是我的解雇动画代码.我基本上是动画表单覆盖视图,以便在解雇时缩小到集合视图单元格的大小.

- (void)_animateDismissingTransitionWithContext:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    UICollectionView *destinationCollectionView = toCollectionViewController.collectionView;
    UICollectionViewCell *destinationCollectionViewCell = [self _destinationCellFromContext:transitionContext];
    UIView *containerView = transitionContext.containerView;

    // Calculate frames    
    CGRect startFrame = fromEventDetailViewController.detailContainerView.frame;
    CGRect endFrame = [destinationCollectionView convertRect:destinationCollectionViewCell.frame toView:containerView];

    // Add overlay
    UIView *overlayView = [UIView new];
    overlayView.backgroundColor = [UIColor overlayBackground];
    overlayView.frame = containerView.bounds;
    overlayView.alpha = 1.0f;
    [containerView addSubview:overlayView];

    // Add fake detail container view
    UIView *fakeContainerView = [UIView new];
    fakeContainerView.backgroundColor = fromEventDetailViewController.detailContainerView.backgroundColor;
    fakeContainerView.frame = startFrame;
    [containerView addSubview:fakeContainerView];

    // Hide from view controller
    fromEventDetailViewController.view.alpha = 0.0f;

    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0f usingSpringWithDamping:0.75f initialSpringVelocity:0.2f options:UIViewAnimationOptionCurveEaseOut animations:^{
        fakeContainerView.frame = endFrame;
        fakeContainerView.backgroundColor = [UIColor eventCellBackground];

        overlayView.alpha = 0.0f;
    } completion:^(BOOL finished) {
        [fromEventDetailViewController.view removeFromSuperview];
        [overlayView removeFromSuperview];
        [fakeContainerView removeFromSuperview];

        [transitionContext completeTransition:YES];
    }];
}
Run Code Online (Sandbox Code Playgroud)

Joh*_*cid 36

另一个解决方案可能是使用beginAppearanceTransition:endAppearanceTransition : . 根据文件:

如果要实现自定义容器控制器,请使用此方法告诉子项其视图将要显示或消失.不要直接调用viewWillAppear:,viewWillDisappear:,viewDidAppear:或viewDidDisappear :.

以下是我如何使用它们:

- (void)animationEnded:(BOOL)transitionCompleted
{
    if (!transitionCompleted)
    {
        _toViewController.view.transform = CGAffineTransformIdentity;
    }
    else
    {
        [_toViewController endAppearanceTransition];
    }
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    [toViewController beginAppearanceTransition:YES animated:YES];
    // ... other code
}
Run Code Online (Sandbox Code Playgroud)

但我仍然认为自定义模态演示没有这样做很奇怪.


mie*_*tus 13

如果您正在使用UIModalPresentationCustom,则应提供自定义UIPresentationController类,如果要使用ViewController生命周期调用者,则需要覆盖shouldRemovePresentersView并返回YES.

如果您希望保留演示者并仍然具有ViewControlelr生命周期回调,则可以覆盖私有方法_shouldDisablePresentersAppearanceCallbacks并返回NO自定义UIPresentationController类.


Chr*_*ris 10

经过多次争论,我发现在ios7和ios8中运行的最佳解决方案是离开,modalPresentationStyle = UIModalPresentationFullScreen而不是UIModalPresentationCustom像文档建议的那样.

如果我这样做以及设置transitioningDelegate我的委托,它仍然尊重我的过渡,在'from'视图控制器上调用will/diddisappear方法.另外:没有现在然后旋转然后解雇旋转问题才能启动.