简体   繁体   English

Swift 从闭包内部添加 VC 不起作用

[英]Swift adding VC from inside a closure not working

So i'm trying to display a loading view when a user tap on a button.所以我试图在用户点击按钮时显示加载视图。 Below is the function下面是function

    /// Handles the shutter button tapped
    @objc func shutterTapped() {
        let parent = self.parent as! CameraActionViewController

        if parent.timerEngine.currentTime != 0 { parent.timerEngine.presentTimerDisplay() }
// This acts as a timer for the shutter to fire. (like a timer function for a camera)
        DispatchQueue.main.asyncAfter(deadline: .now() + Double(parent.timerEngine.currentTime)) {

            parent.cameraManager.capturePictureWithCompletion({ result in
                switch result {
                case .failure:
                    AlertHelper.shared.presentDefault(title: "Error", message: "Looks like there was an error capturing the image.", to: self)

                case .success(let content):
                    let loadingVC = LoadingViewController()
                    parent.parent!.addVC(loadingVC)
                    print("processing...")

                    if let image = content.asImage {
                        guard let editedImage = FilterEngine.shared.process(originalImage: image) else {
                            TapticHelper.shared.errorTaptic()
                            return
                        }

                        guard let data = editedImage.jpegData(compressionQuality: 1.0) else {
                            TapticHelper.shared.errorTaptic()
                            return
                        }

                        DataEngine.shared.save(imageData: data)
                        print("Success")
                        TapticHelper.shared.successTaptic()
                        loadingVC.removeVC()
                    }
                }
            })
        }
    }

When the camera successfully captures the image and start to edit with all the filters selected by the user, I want to display the loading view.当相机成功捕获图像并开始使用用户选择的所有过滤器进行编辑时,我想显示加载视图。 Because this is a heavy task and will freeze the UI for a second.因为这是一项繁重的任务,并且会冻结 UI 一秒钟。

let loadingVC = LoadingViewController()
parent.parent!.addVC(loadingVC)
...
loadingVC.removeVC()

The method to add is here:添加的方法在这里:

    func addVC(_ child: UIViewController) {
        addChild(child)
        view.addSubview(child.view)
        child.didMove(toParent: self)
    }

    func removeVC() {
        guard parent != nil else { return }

        willMove(toParent: nil)
        view.removeFromSuperview()
        removeFromParent()
    }

But for some reason, it is not showing the view at all.但由于某种原因,它根本没有显示视图。 I then found out that it is somehow executed after the DataEngine.shared.save(imageData: data) line (adding to VC).然后我发现它在DataEngine.shared.save(imageData: data)行(添加到 VC)之后以某种方式执行。 Therefore since it is adding and removing at the same time, it shows nothing.因此,由于它是同时添加和删除,所以它什么也不显示。

Any help?有什么帮助吗?

You need to Dispatch all UI related tasks to the main queue.您需要将所有与 UI 相关的任务分派到主队列。 Try:尝试:

DispatchQueue.main.async {
    parent.parent!.addVC(loadingVC)
}

And:和:

DispatchQueue.main.async {
    loadingVC.removeVC()
}

Or simply put everything in the main queue right after the background task is complete:或者只是在后台任务完成后将所有内容放入主队列:

parent.cameraManager.capturePictureWithCompletion({ result in
    DispatchQueue.main.async {
        //...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM