不调用委托方法(transitioningDelegate)

Ant*_*nyR 11 ios swift

我正在尝试在两个viewControllers之间实现自定义转换.(Swift 3.0)

在我的两个viewControllers之间,我有一个UISeguekind show(animated = true).

所以我UIViewControllerTransitioningDelegate在第一个视图控制器的扩展中设置了委托方法:

extension DocumentsViewController : UIViewControllerTransitioningDelegate { ... }
Run Code Online (Sandbox Code Playgroud)

我还通过协议实现了所需的方法:

animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
     ...
}

public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
     ...
}
Run Code Online (Sandbox Code Playgroud)

现在当segue执行时,在firstViewController我使用委托方法prepareForSegue最终设置transitioningDelegate为我的`secondViewController,见下文:

public override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)
    if let destination = segue.destination as? DocumentDetailViewController {
        destination.transitioningDelegate = self
    }
}
Run Code Online (Sandbox Code Playgroud)

我检查断点,委托已经设置为我的firstViewController.但是transitioningDelegate我的firstViewController中的委托方法永远不会被触发,我不知道为什么.

有任何想法吗 ?

PS:在我的故事板中,我的segue have Animated to true,所以这应该有效,但事实并非如此.

谢谢.

解决: MadreDeDios,Matt和Tushar的混合答案.

1:由于我想在我的应用程序中保持导航,我必须使我的第一个viewController符合UINavigationControllerDelegate而不是UIViewControllerTransitioningDelegate.(见MadreDeDios的答案).

2:根据这个协议,我实现了以下委托方法:

public func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {

...

}
Run Code Online (Sandbox Code Playgroud)

3:我早些时候将代表设置viewDidLoad()为我的firstViewController(见Matt的回答):

override public func viewDidLoad() {
    super.viewDidLoad()
    //...
    self.navigationController?.delegate = self
}
Run Code Online (Sandbox Code Playgroud)

4: 我正在使用手动推动而不是segue来显示我secondViewController(请参阅Tushar的回答).

现在这个有效,谢谢.

Mad*_*ios 6

因为你正在使用push segue,我假设你也在使用导航控制器.

当您使用时UINavigationController,它将成为每个过渡,动画甚至应用程序方向的参考.

我的建议是使用导航控制器作为所有动画的管理器.您需要做的就是添加以下几点:

extension MyNavigationController: UINavigationControllerDelegate {

     func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        //Check for the good animation
        return MyAnimation()
    }
}
Run Code Online (Sandbox Code Playgroud)

在你的MyNavigationController课堂里

override func viewDidLoad() {
    super.viewDidLoad()

    //This is the key
    self.delegate = self

    //Only if you want to animate the presentation of your navigation controller itself, the first time it appears:
    self.transitioningDelegate = self
}
Run Code Online (Sandbox Code Playgroud)


mat*_*att 5

问题是你设置transitioningDelegate得太晚了.为时已晚.它需要设置非常的视图控制器的生命周期早期.我建议在视图控制器的初始化程序中设置此属性.