使用自定义过渡时,视图控制器是否会被完全删除?

Tig*_*yan 3 cocoa-touch uiviewcontroller ios

我有两个视图控制器。在第一个我有一个按钮,它以模态方式显示第二个。然后,我通过点击它上的一个按钮来关闭第二个(它会下降)。为了消除过渡,我创建了一个符合 的自定义类UIViewControllerAnimatedTransitioning,因此我为视图控制器使用了自定义过渡动画(在消除过渡期间我需要一个自定义行为)。

我的问题如下:由于我使用了自定义转换,转换完成后我的视图控制器是否仍然被删除,或者它仍然存在但不在屏幕上?如果是的话,会不会影响内存,有多坏?

Rob*_*Rob 5

你说:

转换完成后我的视图控制器是否仍然被删除,或者它仍然存在但不在屏幕上?

这里有两个完全不同的问题。

首先,存在视图控制器层次结构的问题。当你展示一个新的视图控制器时,旧的视图控制器总是保留在视图控制器层次结构中,这样当你回到它时,它仍然会在那里。但是,当您关闭时,被关闭的视图控制器将从视图控制器层次结构中删除,并且(除非您做了一些不寻常的事情,例如将您自己的强引用保留在某处)它将被释放。

其次,视图层次结构还有一个单独的问题。呈现时,UIPresentationController指示呈现视图控制器的视图是否保留在视图层次结构中。默认情况下,它将它保留在视图层次结构中,但通常如果执行模态、全屏“呈现”,您将指定一个UIPresentationController子类,告诉它在转换完成时移除呈现视图控制器的视图。


例如,在进行自定义模式“呈现”转换时,呈现的视图控制器的视图是不透明的并覆盖整个屏幕,那么您UIViewControllerTransitioningDelegate不仅要提供动画控制器,还要指定一个呈现控制器:

func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    return YourAnimationController(...)
}

func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    return YourAnimationController(...)
}

func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
    return PresentationController(presentedViewController: presented, presenting: presenting)
}
Run Code Online (Sandbox Code Playgroud)

并且该演示控制器可能相当小,只是告诉它删除演示者的视图:

class PresentationController: UIPresentationController {
    override var shouldRemovePresentersView: Bool { return true }
}
Run Code Online (Sandbox Code Playgroud)