Advertisement
Guest User

TransitionManager.swift file

a guest
Apr 20th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  TransitionManager.swift
  3. //  Ritvik Choudhary
  4. //
  5. //  Created by Ritvik Choudhary on 20/04/15.
  6. //  Copyright (c) 2015 ritvik1512. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class TransitionManager: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate
  12. {
  13.    
  14.     // MARK: UIViewControllerAnimatedTransitioning protocol methods
  15.    
  16.     // animate a change from one viewcontroller to another
  17.     func animateTransition(transitionContext: UIViewControllerContextTransitioning)
  18.     {
  19.         // TODO: Perform the animation
  20.         // get reference to our fromView, toView and the container view that we should perform the transition in
  21.         let container = transitionContext.containerView()
  22.         let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
  23.         let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
  24.        
  25.         // set up from 2D transforms that we'll use in the animation
  26.         let offScreenRight = CGAffineTransformMakeTranslation(container.frame.width, 0)
  27.         let offScreenLeft = CGAffineTransformMakeTranslation(-container.frame.width, 0)
  28.        
  29.         // start the toView to the right of the screen
  30.         toView.transform = offScreenRight
  31.        
  32.         // add the both views to our view controller
  33.         container.addSubview(toView)
  34.         container.addSubview(fromView)
  35.        
  36.         // get the duration of the animation
  37.  
  38.         let duration = self.transitionDuration(transitionContext)
  39.        
  40.         // perform the animation!
  41.         UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.8, options: nil, animations: {
  42.            
  43.             fromView.transform = offScreenLeft
  44.             toView.transform = CGAffineTransformIdentity
  45.            
  46.             }, completion: { finished in
  47.                
  48.                 // tell our transitionContext object that we've finished animating
  49.                 transitionContext.completeTransition(true)
  50.                
  51.         })
  52.        
  53.     }
  54.    
  55.        
  56.     }
  57.    
  58.     // return how many seconds the transiton animation will take
  59.     func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval
  60.     {
  61.         return 0.5
  62.     }
  63.    
  64.     // MARK: UIViewControllerTransitioningDelegate protocol methods
  65.    
  66.     // return the animataor when presenting a viewcontroller
  67.    
  68.     func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?
  69.     {
  70.         return nil
  71.     }
  72.    
  73.     // return the animator used when dismissing from a viewcontroller
  74.     func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
  75.     {
  76.         return nil
  77.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement