Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 70 lines (51 sloc) 2.77 KB
//
// Created by Pete Callaway on 26/06/2014.
// Copyright (c) 2014 Dative Studios. All rights reserved.
//
import UIKit
class CustomPresentationAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
let isPresenting :Bool
let duration :NSTimeInterval = 0.5
init(isPresenting: Bool) {
self.isPresenting = isPresenting
super.init()
}
// ---- UIViewControllerAnimatedTransitioning methods
func transitionDuration(transitionContext: UIViewControllerContextTransitioning!) -> NSTimeInterval {
return self.duration
}
func animateTransition(transitionContext: UIViewControllerContextTransitioning!) {
if isPresenting {
animatePresentationWithTransitionContext(transitionContext)
}
else {
animateDismissalWithTransitionContext(transitionContext)
}
}
// ---- Helper methods
func animatePresentationWithTransitionContext(transitionContext: UIViewControllerContextTransitioning) {
let presentedController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
let presentedControllerView = transitionContext.viewForKey(UITransitionContextToViewKey)!
let containerView = transitionContext.containerView()!
// Position the presented view off the top of the container view
presentedControllerView.frame = transitionContext.finalFrameForViewController(presentedController)
presentedControllerView.frame.origin.y = containerView.bounds.size.height
containerView.addSubview(presentedControllerView)
// Animate the presented view to it's final position
UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.0, options: .AllowUserInteraction, animations: {
presentedControllerView.center.y = containerView.bounds.size.height/2
}, completion: {(completed: Bool) -> Void in
transitionContext.completeTransition(completed)
})
}
func animateDismissalWithTransitionContext(transitionContext: UIViewControllerContextTransitioning) {
let presentedControllerView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
let containerView = transitionContext.containerView()!
// Animate the presented view off the bottom of the view
UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0.0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .AllowUserInteraction, animations: {
presentedControllerView.frame.origin.y = containerView.bounds.size.height
}, completion: {(completed: Bool) -> Void in
transitionContext.completeTransition(completed)
})
}
}