D. Murphy
D. Murphy

Reputation: 67

Custom Slide Segue - Xcode 8.0 Swift 3.0

What is the best way to create a custom segue that lets me press a button and then a view controller slides on. I have created a left to right segue but I want to make it go the other way. I have looked at some Youtube videos and this question but they don't show me what I want.

My code:

import UIKit

class SegueFromLeft: UIStoryboardSegue
{
    override func perform()
    {
        let src = self.sourceViewController
        let dst = self.destinationViewController

        src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
        dst.view.transform = CGAffineTransformMakeTranslation(-src.view.frame.size.width, 0)

        UIView.animateWithDuration(0.25,
            delay: 0.0,
            options: UIViewAnimationOptions.CurveEaseInOut,
            animations: {
                dst.view.transform = CGAffineTransformMakeTranslation(0, 0)
            },
            completion: { finished in
                src.presentViewController(dst, animated: false, completion: nil)
            }
        )
    }
}

Basically, I want to create a segue that goes from right to left.

Thanks

I am using Xcode 8.0 and Swift 3.0

Upvotes: 3

Views: 3609

Answers (2)

gabriel_vincent
gabriel_vincent

Reputation: 1250

Swift 3.1:

class ProceedToAppStoryboardSegue: UIStoryboardSegue {

    override func perform(){

        let slideView = destination.view

        source.view.addSubview(slideView!)
        slideView?.transform = CGAffineTransform(translationX: source.view.frame.size.width, y: 0)

        UIView.animate(withDuration: 0.25,
                               delay: 0.0,
                               options: UIViewAnimationOptions.curveEaseInOut,
                               animations: {
                               slideView?.transform = CGAffineTransform.identity
        }, completion: { finished in

            self.source.present(self.destination, animated: false, completion: nil)
            slideView?.removeFromSuperview()
        })
    }
}

Upvotes: 0

Marie Dm
Marie Dm

Reputation: 2727

Try this:

class SegueFromLeft: UIStoryboardSegue{
    override func perform(){
        let src=sourceViewController
        let dst=destinationViewController
        let slide_view=destinationViewController.view

        src.view.addSubview(slide_view)
        slide_view.transform=CGAffineTransform.init(translationX: src.view.frame.size.width, 0)

        UIView.animateWithDuration(0.25,
            delay: 0.0,
            options: UIViewAnimationOptions.CurveEaseInOut,
            animations: {
                slide_view.transform=CGAffineTransform.identity
        }, completion: {finished in
                src.present(dst, animated: false, completion: nil)
                slide_view.removeFromSuperview()
        })
    }
}

Upvotes: 4

Related Questions