Search code examples
iosxcodeswiftstoryboardsegue

Set up custom segue way in Swift (top to button direction)


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


Solution

  • 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