Painless custom transitioning. Easy extend, easy setup, just focus on animations.
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
- Download and drop
/TransitionManager
folder in your project. - Congratulations!
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
}
Create a subclass of TransitionManagerAnimation
class FadeTransitionAnimation: TransitionManagerAnimation {
}
TransitionManagerAnimation
class implements TransitionManagerDelegate
protocol.
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 has 3 parts:
- Init
interactionTransitionController
and either pop or push navigation controller when gesture (interaction) starts. - Calculate your
percent
s on gesture change andupdateInteractiveTransition:
with that percent - When gesture ended, decide if your transition complete or not and give information to your
interactionTransitionController
withfinishInteractiveTransition ()
andcancelInteractiveTransition ()
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