Skip to content

Custom view controller transitioning implementation

License

Notifications You must be signed in to change notification settings

azubala/CustomPresenter

Repository files navigation

CustomPresenter

Version License Platform

About

CustomPresenter is an implementation of UIViewControllerAnimatedTransitioning which is a set of methods for implementing the animations for a custom view controller transition (read more here).

This library provides implementation for presentation (CustomControllerPresentationAnimator) and dismissal (CustomControllerDismissAnimator) of view controller. Transition can be customized using presentation context:

protocol CustomControllerPresentationContext {

    // Transition properties
    
    var backgroundViewForPresentation: UIView? { get set }
    var backgroundAlpha: CGFloat { get }
    var duration: TimeInterval { get }
    var animationSpringDumping: CGFloat? { get }
    var animationInitialSpringVelocity: CGFloat? { get }

    // Optional object that controls presentation/dismissal animation

    var animationDriver: CustomControllerPresentationAnimationDriver? { get }
    
    // Final frame of presented view controller or initial frame of dismissed one;
    // `transitionedView` is presented view during presentation, or dismissed one if we are undergoing dismissal

    func controllerFrame(for containerView: UIView, transitionedView: transitionedView) -> CGRect 
}

Setup

To use CustomPresenter with your view controller you need to follow these 3 steps:

  • set modalPresentationStyle to .custom for your view controller
  • create implementation of CustomControllerPresentationContext that will describe transition (don't worry, all properties have default values provided by extension, so little effort is required).
  • make sure that your view controller is becomes transitioningDelegate and implement delegate method:
class MyViewController: UIViewController {
    private class MyPresentationContext: CustomControllerPresentationContext {}

    private var myPresentationContext = MyPresentationContext()
}

extension MyViewController: UIViewControllerTransitioningDelegate {
    func animationController(forPresented presented: UIViewController,
                             presenting: UIViewController,
                             source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return CustomControllerPresentationAnimator(presentationContext: myPresentationContext)
    }

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return CustomControllerDismissAnimator(presentationContext: myPresentationContext)
    }
}

IMPORTANT

Please note that if you are presenting your view controller wrapped in UINavigationController, then you have to make sure that modalPresentationStyle is set to .custom on navigation controller and transitioningDelegate is set to your view controller that implements it as in example above.

Requirements

iOS 8, Swift 4

Installation

Cocoapods

CustomPresenter is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'CustomPresenter'

Carthage

Simply add to your Cartfile:

github "azubala/CustomPresenter"

Author

Aleksander Zubala | zubala.com

License

CustomPresenter is available under the MIT license. See the LICENSE file for more info.