因此,我正在尝试在tvOS 10上的视图控制器之间进行动画过渡。

UIViewControllerTransitioningDelegate协议在tvOS上可用,因此我假设我也可以对其进行动画处理。但是由于某种原因,在呈现新的viewcontroller时不会调用任何函数。

我不知道我在做错什么,因为我在iOS上做的事情基本上完全相同。

这是我正在使用的代码:



展示代码

func showStuff() {
    let viewController = ResultViewController()
    viewController.transitioningDelegate = ResultViewControllerTransitionManager()
    viewController.modalPresentationStyle = .custom
    navigationController?.present(viewController, animated: true, completion: nil)
}




过渡代表

class ResultViewControllerTransitionManager: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {
    var duration = 0.5
    var isPresenting = false

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return duration
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        guard let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from) else {return}
        guard let toView = transitionContext.view(forKey: UITransitionContextViewKey.to) else {return}

        (...)
    }

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresenting = false
        return self
    }

    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresenting = true
        return self
    }
}

最佳答案

您可以在第一个视图控制器中实现UIViewControllerAnimatedTransitioningUIViewControllerTransitioningDelegate协议。您的第一个UIViewController可能具有以下代码:

class ViewController: UIViewController, UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {
    func showStuff() {
        let viewController = ResultViewController()
        viewController.transitioningDelegate = self
        viewController.modalPresentationStyle = .custom
        self.present(viewController, animated: true, completion: nil)
    }
    var duration = 0.5
    var isPresenting = false
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
    }

    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
    }
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {

        return 2.0
    }
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        guard let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from) else {return}
        guard let toView = transitionContext.view(forKey: UITransitionContextViewKey.to) else {return}

    }
}


另外,我从第一个控制器(而不是从navigationController)提供一个新控制器。

关于swift - tvOS上的UIViewControllerTransitioningDelegate没有被调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40402784/

10-12 14:35