UIWebView with Progress Bar

后端 未结 9 1376
攒了一身酷
攒了一身酷 2020-12-08 00:47

Hi I\'m new to programming and I\'m trying to make my first app for iPhones on Xcode. My app contains of a button which opens a UIWebView when pressed and loads up a homepag

9条回答
  •  眼角桃花
    2020-12-08 01:29

    Swift 2.2

    class ViewController: UIViewController,UIWebViewDelegate {
    
        @IBOutlet weak var webView: UIWebView!
        @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
        @IBOutlet weak var myProgressView: UIProgressView!
    
        var myTimer = NSTimer()
        var theBool = Bool()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let url = NSURL(string: "https://www.youtube.com")
            let request = NSURLRequest(URL: url!)
            activityIndicator.hidesWhenStopped = true
            activityIndicator.startAnimating()
            webView.loadRequest(request)
    
        }
        func timerCallback(){
            if theBool {
                if myProgressView.progress >= 1 {
                    myProgressView.hidden = true
                    myTimer.invalidate()
                }else{
                    myProgressView.progress += 0.1
                }
            }else{
                myProgressView.progress += 0.05
                if myProgressView.progress >= 0.95 {
                    myProgressView.progress = 0.95
                }
            }
        }
    
        func webViewDidStartLoad(webView: UIWebView) {
            activityIndicator.startAnimating()
            myProgressView.progress = 0
            theBool = false
            myTimer =  NSTimer.scheduledTimerWithTimeInterval(0.01667,target: self,selector: #selector(ViewController.timerCallback),userInfo: nil,repeats: true)
        }
        func webViewDidFinishLoad(webView: UIWebView) {
            activityIndicator.stopAnimating()
            theBool = true
        }
    }
    

提交回复
热议问题