1
사용자 지정 전환을 수행 할 UINavigationController가 있다고 가정합니다.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
속성을 설정할 수 있도록 확장 얻을 : 편의를 위해, 나는 전환 클래스를 정의 다른 클래스에서 같은 탐색 컨트롤러의 이벤트를 관찰하기를 원한다면? 대리자로 두 개의 객체를 가질 수 없으므로 올바른 패턴은 무엇입니까? 전이 로직이 관측중인 컨트롤러에서 보이거나 사라지는 것과 같은 것을 분리하는 것은 자연 스럽다.