Jae Kim
Jae Kim

Reputation: 675

Custom Segue: Going from Top to Bottom

I am attempting to segue by having the source view controller go from the top to bottom. I can get the transition to go from top to bottom, but as the transition is happening, there is a black rectangle that goes from top to bottom. And once that's finished, the destination view controller pops up. Anybody know how to solve this problem? I am using this tutorial: http://www.appcoda.com/custom-segue-animations/ and changing bits and piece of it to do what I want it to do.

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

    }
}

Upvotes: 0

Views: 2365

Answers (2)

coldembrace
coldembrace

Reputation: 550

For those who are looking for the full solution in 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)
    }
}

Upvotes: 0

Mudith Chathuranga Silva
Mudith Chathuranga Silva

Reputation: 7434

Update your code like this, Is it working correctly now ?

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

Upvotes: 1

Related Questions