自定义 Segue:从上到下

Custom Segue: Going from Top to Bottom

我试图通过让源代码视图控制器从上到下移动来继续。我可以获得从上到下的过渡,但是随着过渡的进行,有一个从上到下的黑色矩形。一旦完成,目标视图控制器就会弹出。有人知道如何解决这个问题吗?我正在使用本教程:http://www.appcoda.com/custom-segue-animations/ 并更改它的一些部分以执行我想要它执行的操作。

import UIKit

class FirstCustomSegue: UIStoryboardSegue {
    override func perform() {
        var firstVCView = self.sourceViewController.view as UIView!
        var secondVCView = self.destinationViewController.view as UIView!

        // Get the screen width and height.
        let screenWidth = UIScreen.mainScreen().bounds.size.width
        let screenHeight = UIScreen.mainScreen().bounds.size.height

        // Specify the initial position of the destination view.
        secondVCView.frame = CGRectMake(0.0, 0, screenWidth, screenHeight)

        // Access the app's key window and insert the destination view above the current (source) one.
        let window = UIApplication.sharedApplication().keyWindow
        window?.insertSubview(secondVCView, aboveSubview: firstVCView)

        // Animate the transition.
        UIView.animateWithDuration(2, animations: { () -> Void in
            firstVCView.frame = CGRectOffset(firstVCView.frame, 0.0, screenHeight)
            secondVCView.frame = CGRectOffset(secondVCView.frame, 0.0, screenHeight)

            }) { (Finished) -> Void in
                self.sourceViewController.presentViewController(self.destinationViewController as! UIViewController,
                    animated: false,
                    completion: nil)
        }

    }
}

像这样更新你的代码,它现在可以正常工作了吗?

secondVCView.frame = CGRectMake(0.0, -screenHeight, screenWidth, screenHeight)

对于那些正在 Swift 4 中寻找完整解决方案的人:

override func perform() {
    let firstVCView = self.source.view
    let secondVCView = self.destination.view

    // Get the screen width and height.
    let screenWidth = UIScreen.main.bounds.size.width
    let screenHeight = UIScreen.main.bounds.size.height

    // Specify the initial position of the destination view.
    secondVCView!.frame = CGRect(x: 0, y: -screenHeight, width: screenWidth, height: screenHeight)

    // Access the app's key window and insert the destination view above the current (source) one.
    let window = UIApplication.shared.keyWindow
    window?.insertSubview(secondVCView!, aboveSubview: firstVCView!)

    // Animate the transition.
    UIView.animate(withDuration: 1, animations: { () -> Void in
        firstVCView?.frame = (firstVCView?.frame.offsetBy(dx: 0, dy: screenHeight))!
        secondVCView?.frame = (secondVCView?.frame.offsetBy(dx: 0, dy: screenHeight))!

    }) { (finished) -> Void in
        self.source.present(self.destination, animated: false, completion: nil)
    }
}