Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wzjmit/f11070fd62c2dfd550786ff013248f74 to your computer and use it in GitHub Desktop.
Save wzjmit/f11070fd62c2dfd550786ff013248f74 to your computer and use it in GitHub Desktop.
Segue from Right (Slide the next controllers view in from right to left (and back))
import UIKit
class UIStoryboardSegueFromRight: UIStoryboardSegue {
override func perform()
{
let src = self.sourceViewController as UIViewController
let dst = self.destinationViewController as UIViewController
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)
}
)
}
}
class UIStoryboardUnwindSegueFromRight: UIStoryboardSegue {
override func perform()
{
let src = self.sourceViewController as UIViewController
let dst = self.destinationViewController as UIViewController
src.view.superview?.insertSubview(dst.view, belowSubview: src.view)
src.view.transform = CGAffineTransformMakeTranslation(0, 0)
UIView.animateWithDuration(0.25,
delay: 0.0,
options: UIViewAnimationOptions.CurveEaseInOut,
animations: {
src.view.transform = CGAffineTransformMakeTranslation(src.view.frame.size.width, 0)
},
completion: { finished in
src.dismissViewControllerAnimated(false, completion: nil)
}
)
}
}

Assign the class UIStoryboardSegueFromRight to the custom segue in the storyboard.

To create an Unwind Segue, you'll need the code below in the first controller. You'll use the segueToMe to connect to the Exit in the second controller. Give that Unwind segue the identifier e.g. "returnToMainViewController".

@IBAction func segueToMe(segue: UIStoryboardSegue) {
// segue back
}
override func segueForUnwindingToViewController(toViewController: UIViewController, fromViewController: UIViewController, identifier: String?) -> UIStoryboardSegue {
if let id = identifier {
if id == "returnToMainViewController" {
let unwindSegue = UIStoryboardUnwindSegueFromRight(identifier: id, source: fromViewController, destination: toViewController)
return unwindSegue
}
}
return super.segueForUnwindingToViewController(toViewController, fromViewController: fromViewController, identifier: identifier)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment