自定义缩放过渡不适用于模态演示

Posted

技术标签:

【中文标题】自定义缩放过渡不适用于模态演示【英文标题】:Custom Zoom Transition not working for a modal presentation 【发布时间】:2014-08-23 19:19:31 【问题描述】:

我正在尝试创建 缩放过渡。当它用于“推动”时,我让它工作得很好。现在我需要为模态转换工作,当然不能只是 2 分钟的修复。

转换是从一个NavigationController 到另一个。我还是不明白当ViewController键指向NavigationControllers时,它们应该指向实际的ViewControllers

这正是我想要的,但所提供的控制器上的 viewWillAppear 永远不会被调用,NavigationBar 也不会出现。

请帮助我。我会给出我必须得到答案的每一分!

这里是动画方法:

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

    UINavigationController *fromNav = (id)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UINavigationController *toNav = (id)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    UIViewController<SWZoomTransitionDelegate> *fromVC= (UIViewController<SWZoomTransitionDelegate> *)fromNav.topViewController;
    UIViewController <SWZoomTransitionDelegate> *toVC =  (UIViewController<SWZoomTransitionDelegate> *)toNav.topViewController;

    UIView * containerView = [transitionContext containerView];
    UIView * fromView = [fromVC view];
    UIView * toView = [toVC view];

    [containerView addSubview:toView];

    UIView * zoomFromView = [fromVC viewForZoomTransition];
    UIView * zoomToView = [toVC viewForZoomTransition];

    UIImageView * animatingImageView = [self initialZoomSnapshotFromView:zoomFromView
                                                         destinationView:zoomToView];

    if ([fromVC respondsToSelector:@selector(initialZoomViewSnapshotFromProposedSnapshot:)])
    
        animatingImageView = [fromVC initialZoomViewSnapshotFromProposedSnapshot:animatingImageView];
    

    animatingImageView.frame = [zoomFromView.superview convertRect:zoomFromView.frame
                                                            toView:containerView];

    fromView.alpha = 1;
    toView.alpha = 0;
    zoomFromView.alpha = 0;
    zoomToView.alpha = 0;
    [containerView addSubview:animatingImageView];

    ZoomAnimationBlock animationBlock = nil;
    if ([fromVC respondsToSelector:@selector(animationBlockForZoomTransition)])
    
        animationBlock = [fromVC animationBlockForZoomTransition];
    

    [UIView animateKeyframesWithDuration:self.transitionDuration
                                   delay:0
                                 options:self.transitionAnimationOption
                              animations:^
                                  animatingImageView.frame = [zoomToView.superview convertRect:zoomToView.frame toView:containerView];
                                  fromView.alpha = 0;
                                  toView.alpha = 1;

                                  if (animationBlock)
                                  
                                      animationBlock(animatingImageView,zoomFromView,zoomToView);
                                  
                               completion:^(BOOL finished) 
                                  if ([transitionContext transitionWasCancelled]) 
                                      [toView removeFromSuperview];
                                      [transitionContext completeTransition:NO];
                                      zoomFromView.alpha = 1;
                                   else 
                                      [fromView removeFromSuperview];
                                      [transitionContext completeTransition:YES];
                                      zoomToView.alpha = 1;
                                  
                                  [animatingImageView removeFromSuperview];
                              ];

【问题讨论】:

经过一些调试,我发现我的过渡永远不会完成,也永远不会崩溃。 【参考方案1】:

我在另一个项目中遇到了类似的问题。根据the WWDC session,调用viewWillAppear: 绝对是预期行为:

所以当一个交互式过渡开始时,UIKit 背后的机制实际上会调用视图,视图会消失,会显示视图控制器,所有你通常习惯的东西控制您的应用程序在屏幕出现和显示时发生的事情。

在我的例子中,问题是动画对象在过渡期间被释放。我是这样设置动画师的:

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
                                                                  presentingController:(UIViewController *)presenting
                                                                        sourceController:(UIViewController *)source 

    AGBAnimator *animator = [AGBAnimator new];
    return animator;

此对象超出范围并在动画期间被释放。通过创建strong属性并在从该方法返回之前为其分配animator,问题得到解决并成功调用viewWillAppear:等。

【讨论】:

好吧,这听起来确实像我的确切情况。我现在要试试。我真的希望它有效:) 不,遗憾的是这仍然不起作用。我想我在某个地方有一个合法的错误。奇怪的是,当我使用过渡来防止没有导航控制器的模态视图控制器时,一切正常。当我添加它时,一切都搞砸了,因此这篇文章。

以上是关于自定义缩放过渡不适用于模态演示的主要内容,如果未能解决你的问题,请参考以下文章

UIViewController 自定义过渡风格(由内而外呈现)

UINavigationController 自定义模态过渡,导航栏太小

如何在尺寸类更改期间从自定义演示控制器过渡到模态?

自定义 inputView 在模态演示期间未正确设置动画

model.predict 不适用于 Keras 自定义层(推理错误)

带有自定义过渡的 presentViewController