自定义segue不会调整子视图的大小

时间:2018-04-12 15:18:59

标签: ios swift uiviewanimation unwind-segue childviewcontroller

我正在使用自定义segue从集合视图单元格转换为具有放大/缩小样式的视图控制器 - 非常像应用程序在当您打开/关闭时,主屏幕

在目标视图控制器中,我添加了两个子视图控制器之一。导航到目标视图控制器工作正常,相应地添加和转换子视图。

enter image description here

解开segue会导致混乱。子视图似乎会在实际视图后面突然显示,并在实际视图的导航完成后立即消失。

enter image description here

视图添加如下:

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)
}

这些是我的看法:

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)
    })
}

0 个答案:

没有答案