简体   繁体   English

如何在iOS Swift中设置延迟?

[英]How to set a delay in iOS Swift?

func create()
        {
            self.Instance!.viewController!.loadCategory(category: 0)

            /*
            repeat
            {
                print("Loading category section!")
            }
            while (self.Instance!.viewController!.isLoading!)
            */
        }

The above code shows that I can have a delay set until isLoading is false; 上面的代码显示我可以设置一个延迟,直到isLoading为false为止。 however, it never becomes false because it locks the thread. 但是,它永远不会成为假,因为它会锁定线程。

How can I have this in the background? 我如何在后台使用它?

Thanks 谢谢

LoadCategory is using wkwebview to load a website's category page and sets isLoading to true when loading the content of the webpage and sets it to false when it is all done loading LoadCategory正在使用wkwebview加载网站的类别页面,并在加载网页内容时将isLoading设置为true,并在完成加载后将其设置为false

 func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)
    {
        print("Finished navigating to url \(String(describing: webView.url))")

        self.isLoading = false
    }

This is my function which sets it to self.isLoading to false after finished loading content. 这是我的函数,它在完成加载内容后将其设置为self.isLoading为false。

Grand Central Dispath 中央大漠

In Swift you can execute a closure after a given delay using Grand Central Dispatch 在Swift中,您可以使用Grand Central Dispatch在给定的延迟后执行关闭

Here's the code 这是代码

DispatchQueue.main.asyncAfter(deadline: .now() + 2) { 
    print("This will be executed 2 seconds in the future")
} 

Don't forget to import Foundation 别忘了import Foundation

A WKWebView can have a navigation delegate ( WKNavigationDelegate ). WKWebView可以具有导航委托( WKNavigationDelegate )。 That delegate can implement a method called webView:didFinishNavigation ( https://developer.apple.com/reference/webkit/wknavigationdelegate/1455629-webview ) which will tell you when the web view is finished loading content. 该委托可以实现一种称为webView:didFinishNavigationhttps://developer.apple.com/reference/webkit/wknavigationdelegate/1455629-webview )的方法,该方法将在网络视图完成加载内容时告诉您。

Based on your code, your viewController can probably be set up as the delegate for the web view and can perform whatever actions you would like after the content is loaded. 根据您的代码,您的viewController可能可以设置为Web视图的委托,并且可以在加载内容后执行您想要的任何操作。

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

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