I have added a view as sub view using the following code
let controller = SideMenuViewController.instantiateViewControllerFromStoryboard(storyBoardName: "Module1") as! SideMenuViewController
UIView.animate(withDuration:0.5, delay: 0.0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
}, completion: {
(finished: Bool) -> Void in
controller.view.tag = 100
let transition = CATransition()
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromLeft
controller.view.layer.add(transition, forKey: nil)
self.view.addSubview(controller.view)
self.addChildViewController(controller)
controller.didMove(toParentViewController: self)
})
The result is a view controller(sidemenu) sliding from left and revealing. I want to remove the added subview with a transition from right to left I have tried removing from superview with animation using the following code
UIView.animate(withDuration:0.5, delay: 0.0, options: UIViewAnimationOptions.curveEaseIn, animations: {}, completion: {
self.view.viewWithTag(100)?.removeFromSuperview()
})
But this does not result in a smooth transition.How can I remove the added subview with a smooth transition,similiar to the way it was shown???
The removeFromSuperview() method is not animatable, what you can do is making alpha goes to 0 and after it has finished you can safely remove from super view.
If you want to get the same effect as in push, you just need to take your code and create the opposite transition. Since it seems that you are using view controllers you can take advantage of the transitions API.
This is a sort of implementation that I used to achieve the same you want but for pushing and popping view controllers in a container, but the animation is the same, just change constraints, to keep the sideview controller thin.