I am developing a library and I want to provide a default custom transition between two view controllers, the user can also provide his own implementation the first idea that comes to my my mind is to override UIViewController
and implement the UIViewControllerTransitioningDelegate
and then users can subclass my CustomTransitionViewController
is it the best way to do it ? any limitations ? is there a more elegant way using just protocols for example with default implementation ?
import UIKit
class CustomTransitionViewController: UIViewController, UIViewControllerTransitioningDelegate {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.transitioningDelegate = self
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil:Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
self.transitioningDelegate = self
}
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return FadeInAnimator(transitionDuration: 0.5, startingAlpha: 0.8)
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return FadeInAnimator(transitionDuration: 0.5, startingAlpha: 0.8)
}
}
You could also just make an extension for the
UIViewController
that does all the things you need and additionally conforms to your protocol, that enables the user to i.e. disable the default transitions.As an example:
How to add stored properties to extensions, you could have a look at this.
All of this, if you can find a way to set the
transitioningDelegate
somehow in the extension, otherwise you'll have to subclass.