【问题标题】:UIViewControllerTransitioningDelegate on tvOS not being calledtvOS 上的 UIViewControllerTransitioningDelegate 未被调用
【发布时间】:2016-11-03 13:30:27
【问题描述】:

所以我正在尝试在 tvOS 10 上的视图控制器之间进行动画转换。

UIViewControllerTransitioningDelegate 协议在 tvOS 上可用,所以我认为我也可以为它制作动画。但是由于某种原因,在展示新的视图控制器时,从来没有调用任何函数。

我不知道我做错了什么,因为我在 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
    }
}

【问题讨论】:

    标签: swift tvos


    【解决方案1】:

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

    【讨论】:

    • 我更喜欢将动画方法放在一个单独的类中。所以我可以在其他地方重用它,并保持原来的视图控制器更干净。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      问题不在于您使用的是navigationController,因此navigationController?.delegate 需要等于您的ResultViewControllerTransitionManager() 吗?

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

      navigationController?.delegate = transitionDelegate

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多