tvOS 上的 UIViewControllerTransitioningDelegate 未被调用
UIViewControllerTransitioningDelegate on tvOS not being called
所以我正在尝试在 tvOS 10 上的 viewcontroller 之间进行动画转换。
tvOS 上提供了 UIViewControllerTransitioningDelegate 协议,所以我想我也可以为它设置动画。但是由于某些原因 none 函数在呈现新的 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
}
}
您可以在第一个视图控制器中实现 UIViewControllerAnimatedTransitioning
和 UIViewControllerTransitioningDelegate
协议。您的第一个 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)。
所以显然在 showStuff()
函数中初始化 animationmanager 是问题所在。将其作为 属性 存储在 Main viewcontroller 中并将其传递确实有效。
不会是因为您使用的是 navigationController,所以 navigationController?.delegate 需要等于您的 ResultViewControllerTransitionManager() 吗?
我们有一个使用推送动画的工作(应该是同一个问题),但我们设置
navigationController?.delegate = transitionDelegate
所以我正在尝试在 tvOS 10 上的 viewcontroller 之间进行动画转换。
tvOS 上提供了 UIViewControllerTransitioningDelegate 协议,所以我想我也可以为它设置动画。但是由于某些原因 none 函数在呈现新的 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
}
}
您可以在第一个视图控制器中实现 UIViewControllerAnimatedTransitioning
和 UIViewControllerTransitioningDelegate
协议。您的第一个 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)。
所以显然在 showStuff()
函数中初始化 animationmanager 是问题所在。将其作为 属性 存储在 Main viewcontroller 中并将其传递确实有效。
不会是因为您使用的是 navigationController,所以 navigationController?.delegate 需要等于您的 ResultViewControllerTransitionManager() 吗?
我们有一个使用推送动画的工作(应该是同一个问题),但我们设置
navigationController?.delegate = transitionDelegate