如何从内存中正确删除VC - swift

时间:2017-12-29 16:13:43

标签: ios swift memory-leaks uiviewcontroller uianimation

我有一个带有主屏幕和许多VC的应用程序。我创建了一个动画类,它负责我的转换,但在检查内存时,使用量不断增加。

这是我的动画

static func showScreen(_ vc: UIViewController, _ start: CGPoint, _ next: UIViewController) {
    // Global variable to determine whether animations are enabled
    if animate {
        DispatchQueue.main.async {
            next.view.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
            next.view.center = start
            vc.view.superview?.insertSubview(next.view, aboveSubview: vc.view)

            UIView.animate(withDuration: 0.3, delay: 0, animations: {
                next.view.transform = CGAffineTransform(scaleX: 0.15, y: 0.15)
                next.view.center = CGPoint(x: screenSize.width/2, y: screenSize.height/2)
            }) { finished in
                UIView.animate(withDuration: 0.3, animations: {
                    next.view.transform = CGAffineTransform.identity
                }) { finished in
                    vc.present(next, animated: false)
                    BlockApp().unblock()
                }
            }
        }
    } else { vc.present(next, animated: false)  }
}

编辑:第二个动画

static func goHome(_ vc: UIViewController, _ b: Bool, _ delay: Double = 0) {
  //  let next = vc.storyboard?.instantiateViewController(withIdentifier: "mainMenu") as! mainMenu
//  After replacing the above line of code with the following, the deinit{} method gets called on the ViewController, but only when returning to it and still not removing it from memory
    let next = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "mainMenu") as! mainMenu
    if animate {
        DispatchQueue.main.async {
            vc.view.superview?.insertSubview(next.view, belowSubview: vc.view)

            var target = CGPoint(x: vc.view.frame.width/2, y: 0)
            UIView.animate(withDuration: 0.25, delay: delay, animations: {
                vc.view.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
            }) { finished in
                if b {
                    if next.collectionView != nil {
                        if let position = next.collectionView.layoutAttributesForItem(at: selectedPosition) {
                            let dest = position.center
                            target = CGPoint(x: dest.x, y: dest.y + next.titleView.frame.height + 80)
                        }
                    }
                }
                UIView.animate(withDuration: 0.25, animations: {
                    vc.view.center = target
                }) { finished in
                    vc.present(next, animated: false)
                    BlockApp().unblock()
                }
            }
        }
    } else { vc.present(next, animated: false)  }
}

该方法将activeVC作为参数,动画的起始位置和要呈现的VC。我知道GC会在需要时发生,但是通过垃圾邮件屏幕,我可以使用1 GB的内存(没有尝试更多,因为花了一点时间,设备大约有3个。)

问题

  1. 如何从内存中删除所有VC并仅保留所有VC         呈现

  2. 在我呈现的VC上,viewWillAppear被调用两次(一次 对于变换,第二次出现)。有没有办法     在制作动画时保持页面预览的同时调用一次?

  3. 试过

      在提出下一个

      后,
    1. vc.dismiss

    2. 使用vc.navigationController.viewControllers删除所有其他控制器,但数组始终为nil

    3. self.presentingViewController?.removeFromParentViewController()添加到提供的VC

0 个答案:

没有答案