Latest 0.3
Homepage https://github.com/cemolcay/TransitionManager
License MIT
Platforms ios 8.0, requires ARC
Authors

Painless custom transitioning. Easy extend, easy setup, just focus on animations.

Installation

CocoaPods

You can use CocoaPods to install TransitionManager by adding it to your Podfile:

platform :ios, '8.0'
use_frameworks!
pod 'TransitionManager'

To get the full benefits import TransitionManager wherever you import UIKit

import UIKit
import TransitionManager

Manually

  1. Download and drop /TransitionManagerfolder in your project.
  2. Congratulations!

Usage

Copy & paste TransitionManager.swift into your project.

  • Declare a TransitionManager object.
  • Init it with a TransitionManagerAnimation
  • Assign it as your navigation controller’s delegate if you use navigation controller.
    • Else assign it as your view controller’s transitioningDelegate.

    var transition: TransitionManager!

    override func viewDidLoad() {
       super.viewDidLoad()

       transition = TransitionManager (transitionAnimation: FadeTransitionAnimation())
       navigationController?.delegate = transition
    }

Creating Transition Animations

Create a subclass of TransitionManagerAnimation

    class FadeTransitionAnimation: TransitionManagerAnimation {

    }

TransitionManagerAnimation class implements TransitionManagerDelegate protocol.

TransitionManagerDelegate
protocol TransitionManagerDelegate {

    /// Transition nimation method implementation
    func transition(
        container: UIView,
        fromViewController: UIViewController,
        toViewController: UIViewController,
        isDismissing: Bool,
        duration: NSTimeInterval,
        completion: () -> Void)

    /// Interactive transitions,
    /// update percent in gesture handler
    var interactionTransitionController: UIPercentDrivenInteractiveTransition? { get set }
}

For transition animation, we should override transition func and write our custom animation in it.


class FadeTransitionAnimation: TransitionManagerAnimation {
    override func transition(
        container: UIView,
        fromViewController: UIViewController,
        toViewController: UIViewController,
        isDismissing: Bool,
        duration: NSTimeInterval,
        completion: () -> Void) {
        if isDismissing {
            closeAnimation(container,
                fromViewController: fromViewController,
                toViewController: toViewController,
                duration: duration,
                completion: completion)
        } else {
            openAnimation(container,
                fromViewController: fromViewController,
                toViewController: toViewController,
                duration: duration,
                completion: completion)
        }
    }    
}

One important part is completion() must be called because the TransitionManager finishes transition after it gets called.

Interaction Transition

Interaction transition has 3 parts:

  • Init interactionTransitionController and either pop or push navigation controller when gesture (interaction) starts.
  • Calculate your percents on gesture change and updateInteractiveTransition: with that percent
  • When gesture ended, decide if your transition complete or not and give information to your interactionTransitionController with finishInteractiveTransition () and cancelInteractiveTransition ()

Easier TransitionManager setup

You can create a TransitionManagerAnimation container enum and give it all your animations

    enum TransitionManagerAnimations {
        case Fade
        case Pull
    }

Write a func that returns correct transition animation in enum

enum TransitionManagerAnimations {
    case Fade
    case Pull

    func transitionAnimation () -> TransitionManagerAnimation {
        switch self {
        case .Fade:
            return FadeTransitionAnimation()
        case .Pull:
            return PullTransitionAnimation()
        }
    }
}

Extend TransitionManager and write a new init method like

extension TransitionManager {
    convenience init(transition: TransitionManagerAnimations) {
        self.init(transitionAnimation: transition.transitionAnimation())
    }
}

Now you can create TransitionManager in your view controller like

transition = TransitionManager(transition: .Pull)
navigationController?.delegate = transition

Latest podspec

{
    "name": "TransitionManager",
    "version": "0.3",
    "summary": "Painless custom transitioning. Easy extend, easy setup, just focus on animations.",
    "description": "TransitionManagern=================nnPainless custom transitioning. Easy extend, easy setup, just focus on animations.nnnUsagen-----nnCopy & paste `TransitionManager.swift` into your project.nn-  Declare a `TransitionManager` object.n-  Init it with a [`TransitionManagerAnimation`](#Create)n-  Assign it as your navigation controller's delegate if you use navigation controller.n  -  Else assign it as your view controller's `transitioningDelegate`.nn``` swiftnntvar transition: TransitionManager!nntoverride func viewDidLoad() {nt   super.viewDidLoad()nnt   transition = TransitionManager (transitionAnimation: FadeTransitionAnimation())nt   navigationController?.delegate = transitionnt}nn```nnnCreating Transition Animations n-----nnnCreate a subclass of `TransitionManagerAnimation`nn``` swiftntclass FadeTransitionAnimation: TransitionManagerAnimation {nnt}n```nn`TransitionManagerAnimation` class implements `TransitionManagerDelegate` protocol.nn##### TransitionManagerDelegate nn``` swiftnntprotocol TransitionManagerDelegate {nnt    func transition (nt        container: UIView,nt        fromViewController: UIViewController,nt        toViewController: UIViewController,nt        duration: NSTimeInterval,nt        completion: ()->Void)nnt    var interactionTransitionController: UIPercentDrivenInteractiveTransition? { get set }nt}nn```nnFor transition animation, we should override `transition` func and write our custom animation in it.nn``` swiftnnclass FadeTransitionAnimation: TransitionManagerAnimation {nn    override func transition (n        container: UIView,n        fromViewController: UIViewController,n        toViewController: UIViewController,n        duration: NSTimeInterval,n        completion: ()->Void) {nn            let fromView = fromViewController.viewn            let toView = toViewController.viewnn            container.addSubview(toView)n            toView.alpha = 0nn            UIView.animateWithDuration(n                duration,n                animations: {n                    toView.alpha = 1n                },n                completion: { finished inn                    completion ()n            })n    }n}nn```nnOne important part is `completion()` must be called because the `TransitionManager` finishes transition after it gets called.nnn### Interaction TransitionnnCreate a `TransitionManagerAnimation` subclass and write an initilizer with `UINavigationController` parameter.nnAdd its `view` a pan gesturenn``` swiftntclass LeftTransitionAnimation: TransitionManagerAnimation {nnt    var navigationController: UINavigationController!nnt    init (navigationController: UINavigationController) {nt        super.init()nnt        self.navigationController = navigationControllernt        self.navigationController.view.addGestureRecognizer(UIPanGestureRecognizer (target: self, action: Selector("didPan:")))nt    }nnt}n```nnWe will update `interactionTransitionController` variable in [`TransitionManagerDelegate`](#Delegate) in gesture handler.nn``` swiftn    func didPan (gesture: UIPanGestureRecognizer) {n        let percent = gesture.translationInView(gesture.view!).x / gesture.view!.bounds.size.widthnn        switch gesture.state {n        case .Began:n            interactionTransitionController = UIPercentDrivenInteractiveTransition()n            navigationController.popViewControllerAnimated(true)nn        case .Changed:n            interactionTransitionController!.updateInteractiveTransition(percent)nn        case .Ended:n            if percent > 0.5 {n                interactionTransitionController!.finishInteractiveTransition()n            } else {n                interactionTransitionController!.cancelInteractiveTransition()n            }n            interactionTransitionController = nilnn        default:n            returnn        }n    }n```nnInteraction transition has 3 parts:n* Init `interactionTransitionController` and either pop or push navigation controller when gesture (interaction) starts.n* Calculate your `percent`s on gesture change and `updateInteractiveTransition:` with that percentn* When gesture ended, decide if your transition complete or not and give information to your `interactionTransitionController` with `finishInteractiveTransition ()` and `cancelInteractiveTransition ()`nnn### Easier `TransitionManager` setupnnYou can create a `TransitionManagerAnimation` container enum and give it all your animationsnn``` swiftntenum TransitionManagerAnimations {nt    case Fadent    case Leftnt}n```nnWrite a func that returns correct transition animation in enumnn``` swiftntenum TransitionManagerAnimations {nt    case Fadent    case Left (UINavigationController)nnt    func transitionAnimation () -> TransitionManagerAnimation {nt        switch self {nt        case .Fade:nt            return FadeTransitionAnimation()nnt        case .Left (let nav):nt            return LeftTransitionAnimation(navigationController: nav)nnt        default:nt            return TransitionManagerAnimation()nt        }nt    }nt}n```nnExtend `TransitionManager` and write a new init method likenn``` swiftnntextension TransitionManager {nnt    convenience init (transition: TransitionManagerAnimations) {nt        self.init (transitionAnimation: transition.transitionAnimation())nt    }nt}nn```nnNow you can create `TransitionManager` in your view controller likenn``` swiftnttransition = TransitionManager (transition: .Left(navigationController!))ntnavigationController?.delegate = transitionn```",
    "homepage": "https://github.com/cemolcay/TransitionManager",
    "license": "MIT",
    "authors": {
        "cemolcay": "[email protected]"
    },
    "social_media_url": "http://twitter.com/cemolcay",
    "platforms": {
        "ios": "8.0"
    },
    "source": {
        "git": "https://github.com/cemolcay/TransitionManager.git",
        "tag": "0.3"
    },
    "source_files": "TransitionManager/Source/*.swift",
    "requires_arc": true
}

Pin It on Pinterest

Share This