View Controller Transitions on Swift
I used to create animation between view controller before i found one of the most exciting iOS 7. I try to create a example project for explain how to use custom animation transition between two view controller. On basic we need to create two thing.
First is UINavigationControllerDelegate and second is UIViewControllerAnimatedTransitioning for custom animation.
NavigationControllerDelegate.swift
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
class NavigationControllerDelegate: NSObject,UINavigationControllerDelegate { @IBOutlet var navigationController: UINavigationController! var interactionController:UIPercentDrivenInteractiveTransition? var animator = CustomAnimator() override func awakeFromNib() { var panRecognizer = UIPanGestureRecognizer(target: self, action: "panGestureAction:") self.navigationController.view.addGestureRecognizer(panRecognizer) } func panGestureAction(recognizer:UIPanGestureRecognizer){ var view = self.navigationController.view if (recognizer.state == UIGestureRecognizerState.Began){ var location = recognizer.locationInView(view) if (location.x < CGRectGetMidX(view.bounds) && self.navigationController.viewControllers.count > 1){ self.interactionController = UIPercentDrivenInteractiveTransition() self.navigationController.popViewControllerAnimated(true) } }else if (recognizer.state == UIGestureRecognizerState.Changed){ var translation = recognizer.translationInView(view) var d = fabs(translation.x / CGRectGetWidth(view.bounds)) self.interactionController?.updateInteractiveTransition(d) }else if (recognizer.state == UIGestureRecognizerState.Ended){ if (recognizer.velocityInView(view).x > 0){ self.interactionController?.finishInteractiveTransition() }else{ self.interactionController?.cancelInteractiveTransition() } self.interactionController = nil } } func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? { // if operation == UINavigationControllerOperation.Pop { // return animator // } return animator } func navigationController(navigationController: UINavigationController, interactionControllerForAnimationController animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? { return self.interactionController } } |
CustomAnimator.swift
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
class CustomAnimator: NSObject, UIViewControllerAnimatedTransitioning { func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval { return 1 } func animateTransition(transitionContext: UIViewControllerContextTransitioning) { var containerView = transitionContext.containerView() var fromViewVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as UIViewController! var toViewVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) as UIViewController! containerView.addSubview(toViewVC!.view) toViewVC?.view.alpha = 0 UIView.animateWithDuration(self.transitionDuration(transitionContext), delay: 0, options: UIViewAnimationOptions.CurveEaseIn, animations: { // fromViewVC?.view.transform = CGAffineTransformMakeScale(0.1, 0.1) fromViewVC?.view.transform = CGAffineTransformRotate(CGAffineTransformIdentity, 1.5) toViewVC?.view.alpha = 1 }, completion: { finished in fromViewVC?.view.transform = CGAffineTransformIdentity var transitionCancelled = transitionContext.transitionWasCancelled() transitionContext.completeTransition(!transitionCancelled) }) } } |
Download Example Project : [fa icon=”github”] https://github.com/igroomgrim/View-Controller-Transitions-on-Swift
Thank nice image from
http://littlevisuals.co/