Nickoflaus Nickoflaus - 6 months ago 22
Swift Question

Create sliding animation in View Swift

I want to make a sliding animation in Swift. I want it to go from the first Controller to the second by sliding down from the first view controller.Example: You're POV is on the firstVC and when you press a button, the firstVC slides down and on top of it is the secondVC. Is that a plausible transition? And if so, how would I accomplish this? Thanks!

Here is an example image of what I'm looking for: Example

Note: I do not want the secondView to slide over the first view, I want it to stay above it (or on top) of the first view as it slides down.


To create this segue, you will need to create a UIStoryBoardSegue file. In that file, put this code:

 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, -screenHeight, screenWidth, screenHeight)
    firstVCView.frame = CGRectMake(0.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(0.4, 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)

Control click to create a segue. Select 'custom'. Then, type in the segue file name in the attributes inspector. Your custom segue is good to go!

Hope this helped.