使用自定义转换委托呈现视图控制器在完成动画后获得黑屏

时间:2017-03-19 03:49:00

标签: ios swift custom-transition

我的目标是使用Transperent背景从第一个视图的顶部为第二个视图控制器视图设置动画。

我正在尝试从第一视图控制器提供第二视图控制器。

创建了一个UIViewControllerAnimatedTransitioning子类,实现的代码是

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

    let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
    let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
    let finalFrameForVC = transitionContext.finalFrame(for: toViewController)
    let containerView = transitionContext.containerView
    let bounds = UIScreen.main.bounds
    //        toViewController.view.frame = CGRectOffset(finalFrameForVC, 0, bounds.size.height)
    toViewController.view.frame = finalFrameForVC.offsetBy(dx: 0, dy: -bounds.size.height)
    containerView.addSubview(toViewController.view)


    UIView.animate(withDuration: 0.5, delay: 0.3, animations: {
        toViewController.view.frame = finalFrameForVC
    }, completion: {
        (value: Bool) in
        transitionContext.completeTransition(true)
        toViewController.view.backgroundColor = UIColor.clear
    })
}

将First View Controller符合UIViewControllerTransitioningDelegate并实现以下

func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?{
    flipPresentAnimationController.originFrame = CGRect(x: 0, y: 0, width: view.frame.width, height: 0)
    return flipPresentAnimationController
}

在第一视图控制器的按钮操作中实现了演示逻辑

let sb = UIStoryboard(name: "Main", bundle: nil)
    let controller = sb.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
    controller.view.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 0)

completion: nil)
    self.navigationController?.pushViewController(controller, animated: true)

每件事情都很好但是在动画完成后我得到了黑屏而不是透明。

注意:如果删除该行,我在第二视图控制器中有一个按钮操作    transitionContext.completeTransition(真)  在自定义类黑屏中没有得到但Button操作没有调用。

0 个答案:

没有答案