Permalink
Switch branches/tags
Nothing to show
Find file Copy path
cb14c01 Oct 31, 2015
2 contributors

Users who have contributed to this file

@PeteC @danielctull
80 lines (59 sloc) 2.84 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) {
guard
let presentedController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey),
let presentedControllerView = transitionContext.viewForKey(UITransitionContextToViewKey),
let containerView = transitionContext.containerView()
else {
return
}
// Position the presented view off the top of the container view
presentedControllerView.frame = transitionContext.finalFrameForViewController(presentedController)
presentedControllerView.center.y -= containerView.bounds.size.height
containerView.addSubview(presentedControllerView)
// Animate the presented view to it's final position
UIView.animateWithDuration(self.duration, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: .AllowUserInteraction, animations: {
presentedControllerView.center.y += containerView.bounds.size.height
}, completion: {(completed: Bool) -> Void in
transitionContext.completeTransition(completed)
})
}
func animateDismissalWithTransitionContext(transitionContext: UIViewControllerContextTransitioning) {
guard
let presentedControllerView = transitionContext.viewForKey(UITransitionContextFromViewKey),
let containerView = transitionContext.containerView()
else {
return
}
// Animate the presented view off the bottom of the view
UIView.animateWithDuration(self.duration, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: .AllowUserInteraction, animations: {
presentedControllerView.center.y += containerView.bounds.size.height
}, completion: {(completed: Bool) -> Void in
transitionContext.completeTransition(completed)
})
}
}