未调用tvOS上的UIViewControllerTransitioningDelegate

时间:2016-11-03 13:30:27

标签: swift tvos

所以我试图在tvOS 10上的viewcontrollers之间进行动画过渡。

在tvOS上可以使用UIViewControllerTransitioningDelegate协议,所以我想我也可以为它设置动画。但由于某些原因,在呈现新的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
    }
}

3 个答案:

答案 0 :(得分:0)

您可以在第一个视图控制器中实现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)提出一个新的控制器。

答案 1 :(得分:0)

显然,在showStuff()函数中初始化动画管理器是个问题所在。将它作为属性存储在主视图控制器中并传递它确实有效。

答案 2 :(得分:0)

问题不是因为您使用的是NavigationController,所以navigationController?.delegate是否需要等于ResultViewControllerTransitionManager()?

我们有一个正在使用推送动画的人(应该是同一个问题),但是我们设置了

navigationController?.delegate = transitionDelegate