1

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.

enter image description here

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.

enter image description here

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)
    })
}
MRizwan33
  • 2,723
  • 6
  • 31
  • 42
ChrisTheGreat
  • 512
  • 4
  • 21
  • so you expect the view cell pointer to remain at a valid frame so you can go back to it? ... id question that first i think. – Daij-Djan Apr 12 '18 at 15:33
  • 1
    also I dont really get it but that might because it is too early in the morning :D I wrote a zoom segue like this once: https://github.com/Daij-Djan/DDUtils/blob/master/swift/ddutils-ios/ui/DDContentZoomSegue%20%5Bios%20%2B%20demo%5D/DDContentZoomSegue.swift – Daij-Djan Apr 12 '18 at 15:38
  • the main difference is I deal with screenshot and animate that and swap it out for the view later. – Daij-Djan Apr 12 '18 at 15:39
  • 1
    That might be a possible suitable solution for me. I will have a look at it. Thank you @Daij-Djan – ChrisTheGreat Apr 12 '18 at 15:43

0 Answers0