3

사용자 정의 모달 프레젠테이션 및 사용자 정의 컨트롤러 (UIViewController의 하위 클래스)가 있습니다. 그것은 자신의 전환 대리자이며 일부 애니메이션 전환 개체 및 프레젠테이션 컨트롤러를 반환합니다. 나는 애니메이트 된 트랜지션 오브젝트를 사용하여 프리젠 테이션시 프리젠 테이션 뷰를 컨테이너 뷰에 추가하고이를 닫을 때 제거하고 물론 애니메이션을 적용합니다. 프레젠테이션 컨트롤러를 사용하여 일부 도우미 하위 뷰를 추가합니다. 내가 presentViewController와 컨트롤러를 제시하고 animated 속성에 true를 전달하면애니메이션이없는 사용자 지정보기 컨트롤러 프레젠테이션

public final class PopoverPresentationController: UIPresentationController { 
    private let touchForwardingView = TouchForwardingView() 

    override public func presentationTransitionWillBegin() { 
     super.presentationTransitionWillBegin() 
     self.containerView?.insertSubview(touchForwardingView, atIndex: 0) 
    } 
} 

public final class PopoverAnimatedTransitioning: NSObject, UIViewControllerAnimatedTransitioning { 

    func setupView(containerView: UIView, presentedView: UIView) { 
     //adds presented view to container view 
    } 

    public func animateTransition(transitionContext: UIViewControllerContextTransitioning) { 
     //1. setup views 
     //2. animate presentation or dismissal 
    } 
} 

public class PopoverViewController: UIViewController, UIViewControllerTransitioningDelegate { 

    init(...) { 
     ... 
     modalPresentationStyle = .Custom 
     transitioningDelegate = self 
    } 

    public func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? { 
     return PopoverAnimatedTransitioning(forPresenting: true, position: position, fromView: fromView) 
    } 

    public func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { 
     return PopoverAnimatedTransitioning(forPresenting: false, position: position, fromView: fromView) 
    } 

    public func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController?, sourceViewController source: UIViewController) -> UIPresentationController? { 
     return PopoverPresentationController(presentedViewController: presented, presentingViewController: presenting, position: position, fromView: fromView) 
    } 

} 

모든 것이 잘 작동합니다. 그러나 애니메이션없이 표시하고 false를 전달하려는 경우 UIKit은 presentationControllerForPresentedViewController 메서드를 호출하기 만하며 animationControllerForPresentedController을 호출하지 않습니다. 제시된 뷰가 뷰 계층 구조에 추가되고 결코 생성되지 않는 애니메이션 전환 객체에 배치되는 한, 아무 것도 제시되지 않습니다.

내가하고있는 일은 전환이 움직이는 지 프리젠 테이션 컨트롤러에서 확인하는 것이며, 그렇지 않은 경우 수동으로 애니메이션 전환하는 객체를 만들고 설정보기로 만듭니다.

override public func presentationTransitionWillBegin() { 
    ... 
    if let transitionCoordinator = presentedViewController.transitionCoordinator() where !transitionCoordinator.isAnimated() { 
     let transition = PopoverAnimatedTransitioning(forPresenting: true, position: position, fromView: fromView) 
     transition.setupView(containerView!, presentedView: presentedView()!) 
    } 
} 

작동하지만 가장 좋은 방법인지는 확실하지 않습니다.

설명서에 따르면 프레젠테이션 컨트롤러는 전환하는 동안 추가 설정이나 애니메이션 작업 만 담당해야하며 프레젠테이션을위한 주 작업은 애니메이션 전환 개체에서 수행해야합니다.

프레젠테이션 컨트롤러에서 항상보기를 설정하고 애니메이션 된 전환 개체에서만 애니메이션을 적용하는 것이 좋습니까?

이 문제를 해결하는 더 좋은 방법이 있습니까?

답변

1

애니메이션 전환에서보기 설정의 모든 논리를 프리젠 테이션 컨트롤러로 이동하여 해결했습니다.