Last active
July 27, 2016 00:13
-
-
Save xcadaverx/2ee3cc3422a29b1c6bd9d12dfd33bfda to your computer and use it in GitHub Desktop.
This file contains 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 TransitioningDelegate: NSObject, UIViewControllerTransitioningDelegate { | |
var openingFrame: CGRect? | |
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? { | |
let presentationAnimator = PresentationAnimator() | |
presentationAnimator.openingFrame = openingFrame! | |
return presentationAnimator | |
} | |
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { | |
let dismissAnimator = DismissalAnimator() | |
dismissAnimator.openingFrame = openingFrame! | |
return dismissAnimator | |
} | |
} | |
class PresentationAnimator: NSObject, UIViewControllerAnimatedTransitioning { | |
var openingFrame: CGRect? | |
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval { | |
return 0.5 | |
} | |
func animateTransition(transitionContext: UIViewControllerContextTransitioning) { | |
let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)! | |
let toViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)! | |
let containerView = transitionContext.containerView() | |
let animationDuration = self.transitionDuration(transitionContext) | |
let fromViewFrame = fromViewController.view.frame | |
UIGraphicsBeginImageContext(fromViewFrame.size) | |
fromViewController.view.drawViewHierarchyInRect(fromViewFrame, afterScreenUpdates: true) | |
let _ = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
let snapshotView = toViewController.view.resizableSnapshotViewFromRect(toViewController.view.frame, afterScreenUpdates: true, withCapInsets: UIEdgeInsetsZero) | |
snapshotView.frame = openingFrame! | |
containerView?.addSubview(snapshotView) | |
toViewController.view.alpha = 0.0 | |
containerView?.addSubview(toViewController.view) | |
UIView.animateWithDuration(animationDuration, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 20.0, options: [], animations: { | |
snapshotView.frame = fromViewController.view.frame | |
}) { (finished) in | |
snapshotView.removeFromSuperview() | |
toViewController.view.alpha = 1.0 | |
transitionContext.completeTransition(finished) | |
} | |
} | |
} | |
// CALLED IN: | |
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { | |
collectionView.deselectItemAtIndexPath(indexPath, animated: true) | |
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! TierCollectionViewCell | |
let attributes = collectionView.layoutAttributesForItemAtIndexPath(indexPath) | |
let attributesFrame = attributes?.frame | |
let frameToOpenFrom = collectionView.convertRect(attributesFrame!, toView: collectionView.superview) | |
transitionDelegate.openingFrame = frameToOpenFrom | |
let detailViewController = storyboard?.instantiateViewControllerWithIdentifier("tierListDetailViewController") as! TierListDetailViewController | |
detailViewController.transitioningDelegate = transitionDelegate | |
detailViewController.modalPresentationStyle = .Custom | |
presentViewController(detailViewController, animated: true, completion: nil) | |
} | |
class DismissalAnimator: NSObject, UIViewControllerAnimatedTransitioning { | |
var openingFrame: CGRect? | |
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval { | |
return 0.5 | |
} | |
func animateTransition(transitionContext: UIViewControllerContextTransitioning) { | |
let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)! | |
let toViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)! | |
let containerView = transitionContext.containerView() | |
let animationDuration = self.transitionDuration(transitionContext) | |
let snapshotView = fromViewController.view.resizableSnapshotViewFromRect(fromViewController.view.bounds, afterScreenUpdates: true, withCapInsets: UIEdgeInsetsZero) | |
containerView?.addSubview(snapshotView) | |
fromViewController.view.alpha = 0.0 | |
UIView.animateWithDuration(animationDuration, animations: { | |
snapshotView.frame = self.openingFrame! | |
snapshotView.alpha = 0.0 | |
}) { (finished) in | |
snapshotView.removeFromSuperview() | |
fromViewController.view.removeFromSuperview() | |
transitionContext.completeTransition(!transitionContext.transitionWasCancelled()) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment