iOS Swift - webview

时间:2017-02-13 09:13:26

标签: ios swift webview uiactivityindicatorview

我在基于webView的屏幕上工作,我希望在加载Web内容时有一个活动指示器。

由于我必须在应用的另一个屏幕中使用相同的活动指示器,因此我将代码放在特定文件中的静态函数中。

活动指标似乎适用于某些网页(简单)网页,但是当我加载更复杂的网页时,我遇到了问题。 活动指标多次重复。 (见下方截图)

在屏幕截图中,第一个活动指示符具有正确的布局,但下面的一个更暗,这意味着其他几个活动指示符已叠加在一起。 然后他们永远不会消失。

说到代码:

我有一个webView和两个控制活动指标的委托方法。

func webViewDidStartLoad(_ webView: UIWebView) {

    DispatchQueue.main.async {

        EBUtil.startActivityIndicator(self)
    }
}

func webViewDidFinishLoad(_ webView: UIWebView) {

    DispatchQueue.main.async {

        EBUtil.stopActivityIndicator(self)

    }
}

我想这是因为多次调用webViewDidStartLoad这一事实。知道如何防止这种行为发生吗?

提前致谢。

爱德华

enter image description here

修改

这是我的VC的完整代码。

class NewsDetailsViewController: UIViewController, UIWebViewDelegate {

//MARK: - @IBOUTLETS

@IBOutlet weak var webView: UIWebView!

//MARK: - VARIABLES 

var url: String!

//MARK: - APP LIFE CYCLE

override func viewDidLoad() {
    super.viewDidLoad()

    if url != nil {

        let urlToLoad = NSURL(string: url)

        webView.loadRequest(NSURLRequest(url: urlToLoad as! URL) as URLRequest)
    }
}

func webViewDidStartLoad(_ webView: UIWebView) {

    DispatchQueue.main.async {

        EBUtil.startActivityIndicator(self)
    }
}

func webViewDidFinishLoad(_ webView: UIWebView) {

    DispatchQueue.main.async {

        EBUtil.stopActivityIndicator(self)

    }
}

}

活动指标开始和停止的代码:

 static func startActivityIndicator(_ sender: UIViewController) {


    print("START ANIMATING")

    if let view = sender.view {

        activityIndicatorBackground = UIView(frame: CGRect(x: view.center.x - 25, y: view.center.y - 25, width: 50, height: 50))
        activityIndicatorBackground.backgroundColor = UIColor.black
        activityIndicatorBackground.layer.zPosition = CGFloat(MAXFLOAT-1)
        activityIndicatorBackground.alpha = 0.6
        activityIndicatorBackground.layer.cornerRadius = 5

        view.addSubview(activityIndicatorBackground)
        activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        activityIndicator.center = view.center
        activityIndicator.hidesWhenStopped = true
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.white
        activityIndicator.layer.zPosition = CGFloat(MAXFLOAT)
        view.addSubview(activityIndicator)
        activityIndicator.startAnimating()
        UIApplication.shared.beginIgnoringInteractionEvents()
    }
}

static func stopActivityIndicator(_ sender: UIViewController) {
    print("STOP ANIMATING")
    activityIndicatorBackground.removeFromSuperview()
    activityIndicator.stopAnimating()
    UIApplication.shared.endIgnoringInteractionEvents()
}

1 个答案:

答案 0 :(得分:0)

试试这个 -

在viewController类中添加以下行 -

    var activityIndicator: UIActivityIndicatorView?

并更新您的startActivityIndicator stopActivityIndicator方法,如下所示 -

  func startActivityIndicator(_ sender: UIViewController) {

    print("START ANIMATING")

    if let view = sender.view {
        if activityIndicator != nil {
           activityIndicator?.removeFromSuperview()
        }
        activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        activityIndicator?.center = view.center
        activityIndicator?.layer.cornerRadius = 10
        activityIndicator?.backgroundColor = UIColor.gray
        activityIndicator?.hidesWhenStopped = true
        activityIndicator?.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.white
        activityIndicator?.layer.zPosition = CGFloat(MAXFLOAT)
        view.addSubview(activityIndicator!)
        activityIndicator?.startAnimating()
        UIApplication.shared.beginIgnoringInteractionEvents()
    }
}


func stopActivityIndicator(_ sender: UIViewController) {
    print("STOP ANIMATING")
    activityIndicator?.stopAnimating()
    activityIndicator?.removeFromSuperview()
    UIApplication.shared.endIgnoringInteractionEvents()
}

希望它对您有用:)