ios - 在自定义模态解除转换后,第一个 View Controller 的生命周期方法不会被调用

标签 ios swift uicollectionview uistoryboardsegue

我有两个 View Controller :viewController1viewController2viewController1 包含一个包含多个单元格的 Collection View 。

它们通过模态转场连接。为了在它们之间导航,我创建了两个动画器,用于显示 viewController2 (showAnimator) 和关闭它 (dismissAnimator)。

dismissAnimator 中,我为 fromView(viewController2 的 View )和 toView( viewController1 的 View )。以下是执行此操作的两种方法:

 private func animateFromViewDisappearance(fromView: UIView?, withDuration duration: TimeInterval, usingTransitionContext transitionContext: UIViewControllerContextTransitioning?) {
    guard let containerView = transitionContext?.containerView, let fromView = fromView else {return}

    UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: {
        fromView.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
        fromView.alpha = 0.0
    }, completion: {_ in

    })
}

 private func animateToViewAppearance(toView: UIView?, withDuration duration: TimeInterval, usingTransistionContext transitionContext: UIViewControllerContextTransitioning?) {
    guard let containerView = transitionContext?.containerView, let toView = toView else {
        return}


    toView.frame = containerView.frame
    toView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
    toView.alpha = 0.0
    containerView.addSubview(toView)

    UIView.animate(withDuration: duration, delay: 0.3, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: {
        toView.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
        toView.alpha = 1.0
    }) { (success) in
        if success {
            transitionContext?.completeTransition(true)
        }
    }
}

这些方法是从 animateTransition(using:) 中调用的。

问题是动画结束后,viewController1 中的 Collection View 单元格不会出现。我只是得到 View Controller 的 View 和 Collection View 本身。我已经检查并注意到 View Controller 的生命周期方法也没有被调用,但是 viewDidLayoutSubviews 确实被调用了。 Collection View 数据源方法也不会被调用,所以这就是单元格不会出现的原因。

我知道我遗漏了一些非常简单的东西,但无法弄清楚是什么。我也在这里检查了一些答案,但没有一个对我有帮助。

如果您知道如何解决这个问题,我将不胜感激。

最佳答案

我花了一些时间终于找到了解决办法。正如 Apple 文档中所说,我们不能直接调用 View Controller 的生命周期方法(好吧,我们可以,但不推荐)。要调用 viewController1 生命周期方法,我从动画师调用 beginAppearanceTransition(_:animated:) 方法:

 toViewController?.beginAppearanceTransition(true, animated: true)

现在,这确实调用了 View Controller 的生命周期方法,但是 View 布局变得一团糟(我认为,那是因为当最初从 viewController1 转换时,我不仅更改了它的位置,还更改了它的规模)。

所以,最后我也找到了解决该问题的方法。为了解决这个问题,当从 viewController1 转换时,我没有为 View Controller 本身(它的位置和比例)设置动画,而是使用它的快照(以下代码在动画器中用于从 viewController1 转换viewController2):

 let snapshot = UIApplication.shared.keyWindow?.snapshotView(afterScreenUpdates: false)

获得快照后,一旦转换开始,我就将快照插入 viewController1 顶部的 containerView,并移除 viewController1 从 super View 。现在,我为模仿 viewController1 的快照制作动画。

这对我来说很好用。也许它对其他人有用。

关于ios - 在自定义模态解除转换后,第一个 View Controller 的生命周期方法不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50844173/

相关文章:

ios - 是否可以在 iOS 应用程序上设置用户自定义通知?

ios - Carthage 更新失败,提示 "Could not find any available simulators for iOS"- Xcode 10.1、macOS Mojave (10.14.2)

iphone - 确定 segue 是否为 unwind segue?

ios - 与 UICollectionView 单元格一起滑动 View

ios - UICollectionView 在自定义键盘扩展中不起作用

ios - 如何触发 Collection View 的 didSelectItemAtIndexPath : on tap while it's scrolling?

ios - 滚动曲线路径

iOS Firestore 'Document path cannot be empty'

ios - AVPlayer 日志机制 - Swift

ios - 如何每天每天重复几个小时的本地通知?