Last active
January 5, 2016 14:06
-
-
Save Kievkao/edc2ad0997ce273b9603 to your computer and use it in GitHub Desktop.
Custom transitioning snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyTransitionDelegate : NSObject, UIViewControllerTransitioningDelegate { | |
| func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? { | |
| return MyAnimatedTransitioning(isPresented: true) | |
| } | |
| func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { | |
| return MyAnimatedTransitioning(isPresented: false) | |
| } | |
| func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? { | |
| return MyPresentationC(presentedViewController: presented, presentingViewController: presenting) | |
| } | |
| } | |
| //---------------- | |
| class MyAnimatedTransitioning: NSObject, UIViewControllerAnimatedTransitioning { | |
| let isPresented: Bool! | |
| let animationDuration: NSTimeInterval = 1.5 | |
| init(isPresented: Bool) { | |
| self.isPresented = isPresented | |
| super.init() | |
| } | |
| func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval { | |
| return self.animationDuration | |
| } | |
| func animateTransition(transitionContext: UIViewControllerContextTransitioning) { | |
| let fromVC: UIViewController? = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) | |
| let toVC: UIViewController? = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) | |
| let containerView: UIView? = transitionContext.containerView() | |
| if self.isPresented == true { | |
| self.animatePresentation(transitionContext, fromVC: fromVC, toVC: toVC, containerView: containerView) | |
| } | |
| else { | |
| self.animateDismissal(transitionContext, fromVC: fromVC, toVC: toVC, containerView: containerView) | |
| } | |
| } | |
| func animatePresentation(transitionContext: UIViewControllerContextTransitioning?, fromVC: UIViewController?, toVC: UIViewController?, containerView: UIView?) { | |
| toVC?.view.frame = (transitionContext?.finalFrameForViewController(toVC!))! | |
| // we have to add toVC to container. In PresentationController or in this class | |
| // transitionContext?.completeTransition(true) | |
| } | |
| func animateDismissal(transitionContext: UIViewControllerContextTransitioning?, fromVC: UIViewController?, toVC: UIViewController?, containerView: UIView?) { | |
| // transitionContext?.completeTransition(true) | |
| } | |
| } | |
| //------------------------- | |
| class MyPresentationC : UIPresentationController { | |
| override func presentationTransitionWillBegin() { | |
| // This should be done in Animated Transition or Presentation Controller! | |
| self.containerView?.addSubview(self.presentedView()!) | |
| } | |
| override func frameOfPresentedViewInContainerView() -> CGRect { | |
| // example | |
| let width: CGFloat = 200.0 | |
| let height: CGFloat = 100.0 | |
| let frame = CGRectMake(self.containerView!.center.x - width/2, CGRectGetMinY(self.containerView!.frame) - height, width, height) | |
| return frame | |
| } | |
| } | |
| // In the presented VC | |
| required init?(coder aDecoder: NSCoder) { | |
| super.init(coder: aDecoder) | |
| self.presentationSetup() | |
| } | |
| override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { | |
| super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) | |
| self.presentationSetup() | |
| } | |
| func presentationSetup() { | |
| self.transitioningDelegate = self.sidePanelTransitionDelegate | |
| self.modalPresentationStyle = .Custom | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment