1

I want to build a segue between two scenes in storyboard that behaves exactly the same to the "Apple Keynote" effect move in (top to bottom direction). Now I know there's a segue called modally segue, but that works in bottom to top direction.

Please help me!

I made a video on what kind of segue I want, please check it out! https://www.dropbox.com/s/zqyxrm638ellfbx/whatiwant.mov?dl=0

Tom Xue
  • 470
  • 1
  • 7
  • 18

2 Answers2

4

you could implement a custom UIStoryboardSegue like this:

class TopDownSegue: UIStoryboardSegue {
    let duration: NSTimeInterval = 1
    let delay: NSTimeInterval = 0
    let animationOptions: UIViewAnimationOptions = [.CurveEaseInOut]

    override func perform() {
        // get views
        let sourceView = sourceViewController.view
        let destinationView = destinationViewController.view

        // get screen height
        let screenHeight = UIScreen.mainScreen().bounds.size.height
        destinationView.transform = CGAffineTransformMakeTranslation(0, -screenHeight)

        // add destination view to view hierarchy
        UIApplication.sharedApplication().keyWindow?.insertSubview(destinationView, aboveSubview: sourceView)

        // animate
        UIView.animateWithDuration(duration, delay: delay, options: animationOptions, animations: { 
            destinationView.transform = CGAffineTransformIdentity
            }) { (_) in
                self.sourceViewController.presentViewController(self.destinationViewController, animated: false, completion: nil)
        }
    }
}

and to use your new custom segue in storyboard:

custom segue

André Slotta
  • 13,774
  • 2
  • 22
  • 34
  • Thank you very much! You solved my problem! Can you help a little bit on another problem that struggles me so much? http://stackoverflow.com/questions/37900927/auto-layout-constrain-confusion – Tom Xue Jun 18 '16 at 19:39
  • Great example. When I use this I lose my navigation controller "< Back" how do I add this to the navigation stack? – Paul S. Feb 21 '17 at 21:05
  • Thank you, your example helped me to understand a lot about custom segues. – FelipeOliveira Mar 08 '18 at 18:27
0

Since the accepted code is outdated, here the updated version:

class SegueFromTop: UIStoryboardSegue {
    override func perform() {
        let src = self.source
        let dst = self.destination

        src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
        dst.view.transform = CGAffineTransform(translationX: 0, y: -src.view.frame.size.height)

        UIView.animate(withDuration: 0.25,
                       delay: 0.0,
                       options: .curveEaseInOut,
                       animations: {
                        dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
        },
                       completion: { finished in
                        src.present(dst, animated: false, completion: nil)
        }
        )
    }
}
Tom
  • 3,672
  • 6
  • 20
  • 52