为什么我的弹出窗口延迟(Swift)?

时间:2018-05-27 19:02:39

标签: swift popup delay

我是Swift程序员的第一个应用程序。作为一个简单游戏的一部分,我在中间的某个地方执行一个函数(F)和下面的代码

if let vc = storyboard?.instantiateViewController(withIdentifier: 
"P2CompetitionPopUpId") as? P2_Competition_Pop_Up {
            vc.modalPresentationStyle = .overCurrentContext
            present(vc, animated: true, completion: nil)
        } else {
            print("error creating P2_Competion_Pop_Up")

然而,当我运行它时,弹出不会发生,直到整个功能(F)执行完毕。为什么是这样?如何在弹出窗口弹出时暂停功能(F)并在弹出窗口关闭后恢复?

1 个答案:

答案 0 :(得分:0)

将其包装成异步调用:

DispatchQueue.main.async {
    if let vc = self.storyboard?.instantiateViewController(withIdentifier: "P2CompetitionPopUpId") as? P2_Competition_Pop_Up {
        vc.modalPresentationStyle = .overCurrentContext
        self.present(vc, animated: true, completion: nil)
    } else {
        print("error creating P2_Competion_Pop_Up")
}