Skip to content

Instantly share code, notes, and snippets.

@psaitu
Created July 5, 2016 12:42
Show Gist options
  • Select an option

  • Save psaitu/7b3349b6e9d2ea735cd60f3bd73ed9f7 to your computer and use it in GitHub Desktop.

Select an option

Save psaitu/7b3349b6e9d2ea735cd60f3bd73ed9f7 to your computer and use it in GitHub Desktop.
import Foundation
import UIKit
class PopUpTransition: NSObject, UIViewControllerAnimatedTransitioning {
private var presenting:Bool = false
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 0.4
}
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
let from = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
let to = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
let container = transitionContext.containerView()!
let bounds = UIScreen.mainScreen().bounds
let offsetCenter = CGPoint(x: bounds.width / 2, y: bounds.height + 300)
if presenting {
to.view.center = offsetCenter
container.addSubview(to.view)
UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: .CurveLinear, animations: {
from.view.alpha = 0.5
to.view.center = from.view.center
}, completion: {
finished in
transitionContext.completeTransition(true)
})
} else {
container.addSubview(from.view)
//container.addSubview(to.view)
UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: .CurveLinear, animations: {
from.view.center = offsetCenter
to.view.alpha = 1.0
}, completion: {
finished in
transitionContext.completeTransition(true)
})
}
}
func animationEnded(transitionCompleted: Bool) {
print("Transition Completed")
}
}
extension PopUpTransition:UIViewControllerTransitioningDelegate {
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
self.presenting = true
return self
}
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
self.presenting = false
return self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment