My goal here is animate a second View controller view from the top of the First view with Transperent background.
I am trying to present a Second View Controller from First View Controller.
Created a UIViewControllerAnimatedTransitioning sub class and code implemented is
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
})
}
conform the First View Controller to UIViewControllerTransitioningDelegate and implemented the below
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
}
In the button action of the First View Controller Implemented the Presentation logic
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)
Every thing works fine but after completion of the animation I am getting the Black screen instead of the Transparent.
Note: I have a button actions in the Second View Controller, if I delete the line transitionContext.completeTransition(true) in Custom class black screen is not getting but Button actions are not calling.