Skip to content

Instantly share code, notes, and snippets.

@wisaruthk
Last active March 1, 2019 04:30
Show Gist options
  • Save wisaruthk/0fa43afadb15bf98493b to your computer and use it in GitHub Desktop.
Save wisaruthk/0fa43afadb15bf98493b to your computer and use it in GitHub Desktop.
ViewController Transition in swift
func transitionToChildViewController(toViewController:UIViewController){
var fromViewController:UIViewController?
if(self.childViewControllers.count > 0){
fromViewController = self.childViewControllers[0]
} else {
fromViewController = nil;
}
if(toViewController == fromViewController || !self.isViewLoaded()){
return
}
let toView:UIView = toViewController.view;
toView.translatesAutoresizingMaskIntoConstraints = true;
toView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth,UIViewAutoresizing.FlexibleHeight]
toView.frame = self.view.bounds
fromViewController?.willMoveToParentViewController(nil);
self.addChildViewController(toViewController);
// If this is the initial presentation, add the new child with no animation.
if(fromViewController == nil){
self.view.addSubview(toViewController.view)
toViewController.didMoveToParentViewController(self)
return;
}
// default animation
// self.transitionFromViewController(fromViewController!, toViewController: toViewController, duration: 1.0, options: UIViewAnimationOptions.TransitionFlipFromBottom,animations: nil) { (finished:Bool) -> Void in
// //
// fromViewController!.removeFromParentViewController()
// toViewController.didMoveToParentViewController(self)
//
// }
let width:CGFloat! = self.view.frame.width
let height:CGFloat! = fromViewController?.view.frame.height
toViewController.view.frame = CGRectMake(width, 0, width, height)
self.transitionFromViewController(fromViewController!, toViewController: toViewController, duration: 0.5, options: UIViewAnimationOptions.TransitionNone, animations: { () -> Void in
//animation
fromViewController!.view.frame = CGRectMake(0-width, 0, width, height)
toViewController.view.frame = CGRectMake(0,0,width,height)
}) { (finished:Bool) -> Void in
//completion
fromViewController?.removeFromParentViewController()
toViewController.didMoveToParentViewController(self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment