本文介绍了UIViewController动画转换在iOS8中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的UIViewController转换 - 在iOS7中,它运行得很好。

I've got a custom UIViewController transition -- in iOS7, it works perfectly fine.

然而,当在iOS 8上运行时,我遇到了问题。具体来说,当呈现的视图控制器被解除时,原始视图控制器的视图完全消失 - 留下一个完全空白的屏幕。这是我的代码:

However, when run on iOS 8, I'm experiencing issues. Specifically, when the presented view controller is dismissed, the original view controller's view disappears entirely - leaving me with a completely blank screen. Here is my code:

@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中不再有效?

Any idea why this no longer works in iOS8?

谢谢!

推荐答案

以下代码在xcode GM中正常工作:

the following code work fine in 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中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 08:30