我的目标是从具有透明背景的第一个视图顶部为第二个视图控制器视图设置动画。
我正在尝试从第一个视图控制器呈现第二个视图控制器。
创建了一个 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
})
}
使第一个视图控制器符合 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
}
在First View Controller的按钮动作中实现了Presentation逻辑
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(true) 没有得到但按钮操作没有调用。