UIViewController 动画过渡在 iOS8 中失败

Posted

技术标签:

【中文标题】UIViewController 动画过渡在 iOS8 中失败【英文标题】:UIViewController Animated Transitioning fails in iOS8 【发布时间】:2014-09-16 23:19:33 【问题描述】:

我有一个自定义 UIViewController 转换 - 在 ios7 中,它工作得非常好。

但是,在 iOS 8 上运行时,我遇到了问题。具体来说,当呈现的视图控制器被关闭时,原始视图控制器的视图会完全消失——让我看到一个完全空白的屏幕。这是我的代码:

@implementation DDCardTransition

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext 
    return 0.5f;


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

    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    CGRect endFrame = self.destinationFrame;

    if (self.presenting) 
        fromViewController.view.userInteractionEnabled = NO;
        toViewController.view.layer.cornerRadius = 6.0f;

        UIView *tintAdjustmentView = [UIView new];
        tintAdjustmentView.frame = fromViewController.view.frame;
        tintAdjustmentView.backgroundColor = [UIColor blackColor];
        tintAdjustmentView.alpha = 0.f;

        [transitionContext.containerView addSubview:fromViewController.view];
        [transitionContext.containerView addSubview:tintAdjustmentView];
        [transitionContext.containerView addSubview:toViewController.view];

        CGRect startFrame = endFrame;
        startFrame.origin.y += [UIScreen mainScreen].bounds.size.height;
        toViewController.view.frame = startFrame;

        [CATransaction begin];
        [CATransaction setCompletionBlock:^
            [transitionContext completeTransition:YES];
        ];

        CABasicAnimation *frameAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
        frameAnimation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.1f :0.8f :0.0 :1.0];
        frameAnimation.duration = [self transitionDuration:transitionContext];
        frameAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(CGRectGetMidX(startFrame), CGRectGetMidY(startFrame))];
        frameAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(CGRectGetMidX(endFrame), CGRectGetMidY(endFrame))];
        toViewController.view.layer.position = [frameAnimation.toValue CGPointValue];
        [toViewController.view.layer addAnimation:frameAnimation forKey:@"position"];

        CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
        opacityAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        opacityAnimation.duration = 0.3f;
        opacityAnimation.fromValue = @(0.f);
        opacityAnimation.toValue = @(0.5f);
        tintAdjustmentView.layer.opacity = [opacityAnimation.toValue floatValue];
        [tintAdjustmentView.layer addAnimation:opacityAnimation forKey:@"opacity"];

        [CATransaction commit];
    
    else 
        toViewController.view.userInteractionEnabled = YES;

        UIView *tintAdjustmentView = [UIView new];
        tintAdjustmentView.frame = toViewController.view.frame;
        tintAdjustmentView.backgroundColor = [UIColor blackColor];
        tintAdjustmentView.alpha = 0.5f;

        [transitionContext.containerView addSubview:toViewController.view];
        [transitionContext.containerView addSubview:tintAdjustmentView];
        [transitionContext.containerView addSubview:fromViewController.view];

        endFrame.origin.y += [UIScreen mainScreen].bounds.size.height;

        [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^
            fromViewController.view.frame = endFrame;
            tintAdjustmentView.alpha = 0.f;
         completion:^(BOOL finished) 
            [transitionContext completeTransition:YES];
        ];
    


@end

知道为什么这在 iOS8 中不再有效吗?

谢谢!

【问题讨论】:

【参考方案1】:

以下代码在 xcode GM 中运行良好:

func animateTransition(transitionContext: UIViewControllerContextTransitioning!)  

    let fromViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
    let fromViewControllerView = transitionContext.viewForKey(UITransitionContextFromViewKey)
    let containerView = transitionContext.containerView()

    fromViewControllerView?.frame = transitionContext.finalFrameForViewController(fromViewController!)
    fromViewControllerView?.frame.origin.y = containerView.bounds.height

    containerView.addSubview(fromViewControllerView!)

    UIView.animateWithDuration(transitionDuration(transitionContext),
        delay: 0.0,
        options: .AllowUserInteraction,
        animations: 
            fromViewControllerView!.center.y = containerView.bounds.size.height/2
        ,
        completion:  (completed: Bool) -> Void in
            transitionContext.completeTransition(completed)
        
    )

【讨论】:

以上是关于UIViewController 动画过渡在 iOS8 中失败的主要内容,如果未能解决你的问题,请参考以下文章

同时 UIViewController 过渡动画

iOS:自定义后退按钮过渡动画

UIViewController 默认过渡动画不起作用

在 2 个 UIViewController 的过渡之间为 UIView 元素设置动画

自定义 UIViewController 过渡动画适用于演示,但不适用于解雇

iOS7,有没有办法在 UINavigationViewcontroller 中禁用 UIViewController 的过渡?