当分离到从 NSObject 继承的自定义类时,TransitioningDelegate 不适用于 UIPresentationController
Posted
技术标签:
【中文标题】当分离到从 NSObject 继承的自定义类时,TransitioningDelegate 不适用于 UIPresentationController【英文标题】:TransitioningDelegate doesn't work for UIPresentationController when separated to a custom class inherited from NSObject 【发布时间】:2015-06-29 07:08:07 【问题描述】:我使用自定义 UIPresentationController 来执行我自己的动画的续集。在prepareForSegue:
myDestinationController.transitioningDelegate = DBViewControllerTransitioningDelegate()
myDestinationController.modalPresentationStyle = .Custom
这是我的DBViewControllerTransitioningDelegate
:
class DBViewControllerTransitioningDelegate: NSObject, UIViewControllerTransitioningDelegate
func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController?
return DBOverlayPresentationController(presentedViewController: presented, presentingViewController: presenting)
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?
return DBTransitioningAnimator()
它不起作用,因为没有调用方法。但是当我设置时:
myDestinationController.transitioningDelegate = self
在我的self
控制器中添加我的DBViewControllerTransitioningDelegate
中的2 个方法,一切都很好。这两种方法被调用。为什么?有什么区别?
【问题讨论】:
您可能需要事先实例化您的动画师。就像在您的委托类的init()
中一样
【参考方案1】:
在
UIViewController
中声明transitioningDelegate
:
weak var transitioningDelegate: UIViewControllerTransitioningDelegate?
如您所见,transitioningDelegate
持有 weak
引用。自定义的 TransitioningDelegate 实例在您创建后立即发布,因为这里没有人拥有所有权。当您在“控制器”中采用委托并将其分配给 transitioningDelegate
时,“某人”正在为您保留此委托实例。
【讨论】:
以上是关于当分离到从 NSObject 继承的自定义类时,TransitioningDelegate 不适用于 UIPresentationController的主要内容,如果未能解决你的问题,请参考以下文章