1
カスタムトランジションを実行するUINavigationControllerがあるとします。2人の代理人を持つUINavigationController?
extension WTPTransition : UINavigationControllerDelegate
{
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}
func navigationController(_ navigationController: UINavigationController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
return interactionController
}
}
十分に機能
が、何が起こる:
class WTPTransition: NSObject
{
// This is our Interaction Controller, which conforms to UIViewControllerInteractiveTransitioning. Apple's concrete class suits our needs.
var interactionController : UIPercentDrivenInteractiveTransition?
}
extension WTPTransition : UIViewControllerAnimatedTransitioning
{
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 1
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let toView = transitionContext.viewController(forKey: .to)!.view!
let fromView = transitionContext.viewController(forKey: .from)!.view!
transitionContext.containerView.insertSubview(toView, belowSubview: fromView)
UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
fromView.alpha = 0.0
}, completion: { finished in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
})
}
}
は、その後、私は私の遷移を、私は単にUINavigationControllerのdelegate
プロパティを設定することができるようにそれを拡張し得る:利便性のために、私は遷移クラスを定義します同じナビゲーションコントローラのイベントを別のクラスで観察してもらいたいのですか?私はデリゲートとして2つのオブジェクトを持つことができないので、使用する正しいパターンは何ですか?トランジションロジックのようなものを観測するコントローラから切り離すことは当然のようです。