Custom Segue:从上到下

时间:2015-09-18 04:24:52

标签: ios xcode swift segue

我试图通过让源视图控制器从上到下进行调整。我可以从上到下进行过渡,但随着过渡的发生,有一个从上到下的黑色矩形。一旦完成,目标视图控制器就会弹出。有谁知道如何解决这个问题?我正在使用这个教程: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)
        }

    }
}

2 个答案:

答案 0 :(得分:1)

像这样更新您的代码,它现在正常工作吗?

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

答案 1 :(得分:0)

对于那些正在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)
    }
}