Skip to content

Instantly share code, notes, and snippets.

@agibson73
Created March 17, 2015 02:23
Show Gist options
  • Save agibson73/105172fc5bcfbc590694 to your computer and use it in GitHub Desktop.
Save agibson73/105172fc5bcfbc590694 to your computer and use it in GitHub Desktop.
Custom Transition that zooms in then out
//
// TransitionZoomForward.swift
// Created by Alex Gibson on 3/14/15.
// Free to Use by Anyone
// Have Fun!
//
import UIKit
public class TransitionZoomForward: NSObject, UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {
var isPresenting = true
var duration = 0.4
public func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
let container = transitionContext.containerView()
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
if isPresenting {
// we will use this so we do not transform the original View if it contains a collectionView
var viewSnapshot = fromView.snapshotViewAfterScreenUpdates(true)
container.addSubview(viewSnapshot)
container.addSubview(fromView)
//hide the view with the collection View but it is replaced by the snapshot
// appearing as it is still there
fromView.alpha = 0
container.addSubview(toView)
toView.alpha = 0
toView.transform = CGAffineTransformMakeScale(2, 2)
UIView.animateWithDuration(duration, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
viewSnapshot.transform = CGAffineTransformMakeScale(0.5, 0.5)
viewSnapshot.alpha = 0
toView.transform = CGAffineTransformIdentity
toView.alpha = 1
}, completion: { finished in
//clean up and remove snapshot fully even though it is already hidden
viewSnapshot.removeFromSuperview()
})
}
else {
//have to do this to take screenshot because the view is currently hidden
toView.alpha = 1
// use snapshot so we do not mess up our collection layout
var viewSnapshot = toView.snapshotViewAfterScreenUpdates(true)
toView.alpha = 0
viewSnapshot.transform = CGAffineTransformMakeScale(0.25, 0.25)
viewSnapshot.alpha = 0
container.addSubview(viewSnapshot)
container.addSubview(fromView)
//add destination view but the alpha is zero
// this will keep it from jerking with alpha is 1
container.addSubview(toView)
container.sendSubviewToBack(toView)
UIView.animateWithDuration(duration, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
fromView.transform = CGAffineTransformMakeScale(2, 2)
fromView.alpha = 0
viewSnapshot.alpha = 1
viewSnapshot.transform = CGAffineTransformIdentity
}, completion: { finished in
toView.alpha = 1
viewSnapshot.removeFromSuperview()
})
}
delay(duration, {
transitionContext.completeTransition(true)
})
}
public func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
return duration
}
public func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
isPresenting = true
return self
}
public func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
isPresenting = false
return self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment