2016-11-03 162 views
0

所以我試圖做tvOS 10UIViewControllerTransitioningDelegate不會被調用

的UIViewControllerTransitioningDelegate協議可在tvOS,所以我認爲我可以製作動畫以及viewcontrollers之間的動畫過渡。但出於某種原因,在展示新的視圖控制器時,沒有任何功能會被調用。

我不知道我在做什麼錯,因爲我在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 
    } 
} 

回答

0

所以顯然初始化showStuff()函數中的animationmanager是個問題。將它作爲一個屬性存儲在主視圖控制器中,並傳遞給它。

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)提出了一種新的控制器。

+0

我更喜歡將動畫方法保留在單獨的類中。所以我可以在其他地方重複使用它,並保持原始視圖控制器更加清潔。 – Houwert