I am using a custom segue for transitioning from a collection view cell to a view controller with a kind of zoom in/zoom out style - pretty much like App's do it on the Homescreen, when you open/close them.
In the destination view controller I am adding one of two child view controller. Navigation to the destination view controller works fine, subviews are added and transformed accordingly.
Unwinding the segue however results in a mess. The child view seems to snap behind the actual view and disappears as soon as the actual view's navigation is completed.
Views are added like:
private lazy var documentViewController: DocumentViewController = {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
var viewController = storyboard.instantiateViewController(withIdentifier: "DocumentViewController") as! DocumentViewController
self.add(asChildViewController: viewController)
return viewController
}()
override func viewDidLoad() {
super.viewDidLoad()
add(asChildViewController: documentViewController)
}
private func add(asChildViewController viewController: UIViewController) {
// Add Child View Controller
addChildViewController(viewController)
// Add Child View as Subview
view.addSubview(viewController.view)
view.sendSubview(toBack: viewController.view)
// Configure Child View
viewController.view.frame = view.bounds
// Notify Child View Controller
viewController.didMove(toParentViewController: self)
}
These are my segues:
func zoom(_ viewCell: UICollectionViewCell) {
let toViewController = self.destination
let fromViewController = self.source
let containerView = fromViewController.view.superview
let cellContentView = viewCell.contentView
let rect = cellContentView.convert(cellContentView.frame, to: toViewController.view)
let originalCenter = CGPoint(x: rect.origin.x + rect.size.width / 2, y: rect.origin.y + rect.size.height / 2)
let scaleX = rect.size.width / toViewController.view.frame.width
let scaleY = rect.size.height / toViewController.view.frame.height
toViewController.view.transform = CGAffineTransform(scaleX: scaleX, y: scaleY)
toViewController.view.layer.cornerRadius = 50
toViewController.view.center = originalCenter
containerView?.addSubview(toViewController.view)
UIView.animate(withDuration: 10.5, delay: 0, options: .curveEaseInOut, animations: {
toViewController.view.transform = CGAffineTransform.identity
toViewController.view.center = fromViewController.view.center
toViewController.view.layer.cornerRadius = 0
}, completion: { success in
fromViewController.present(toViewController, animated: false, completion: nil)
})
}
func unwindZoom(_ viewCell: UICollectionViewCell) {
let toViewController = self.destination
let fromViewController = self.source
fromViewController.view.superview?.insertSubview(toViewController.view, at: 0)
UIView.animate(withDuration: 10.5, delay: 0, options: .curveEaseInOut, animations: {
let cellContentView = viewCell.contentView
let rect = cellContentView.convert(cellContentView.frame, to: toViewController.view)
let originalCenter = CGPoint(x: rect.origin.x + rect.size.width / 2, y: rect.origin.y + rect.size.height / 2)
let scaleX = rect.size.width / toViewController.view.frame.width
let scaleY = rect.size.height / toViewController.view.frame.height
fromViewController.view.transform = CGAffineTransform(scaleX: scaleX, y: scaleY)
fromViewController.view.center = originalCenter
fromViewController.view.layer.cornerRadius = 50
}, completion: { success in
fromViewController.dismiss(animated: false, completion: nil)
})
}