Tom Xue
Tom Xue

Reputation: 500

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

Upvotes: 1

Views: 1544

Answers (2)

Tom
Tom

Reputation: 4033

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

Upvotes: 0

André Slotta
André Slotta

Reputation: 14040

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

Upvotes: 4

Related Questions