Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NVActivityIndicatorView only for particular view

I am using this library https://github.com/ninjaprox/NVActivityIndicatorView for showing the loading indicator. By default it blocks the entire view controller but how can I show the activity indicator only for the particular view. For example if a view controller contains a webview and some other view, the activity indicator should be only for the webview and I should be able interact with the other view.

class FaqViewController: UIViewController, UIWebViewDelegate
{

    @IBOutlet var faqWebView: UIWebView!
    static let activityData = ActivityData()
    var activityIndicator : NVActivityIndicatorView!

    override func viewDidLoad()
    {
        super.viewDidLoad()
        faqWebView.delegate = self
        faqWebView.loadRequest(URLRequest(url: URL(string: "https://www.google.com")!))
    }

    func webViewDidStartLoad(_ webView: UIWebView)
    {
        NVActivityIndicatorView.DEFAULT_BLOCKER_SIZE = CGSize(width: 45, height: 45)
        NVActivityIndicatorPresenter.sharedInstance.startAnimating(FaqViewController.activityData)
    }

    func webViewDidFinishLoad(_ webView: UIWebView)
    {
        NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
    }
}
like image 222
Prabu Raj Avatar asked Sep 15 '17 07:09

Prabu Raj


1 Answers

for e.g show the activityIndicator

func webViewDidStartLoad(_ webView: UIWebView)
{
    //NVActivityIndicatorView.DEFAULT_BLOCKER_SIZE = CGSize(width: 45, height: 45)
    //NVActivityIndicatorPresenter.sharedInstance.startAnimating(FaqViewController.activityData)

    let xAxis = self.view.center.x // or use (view.frame.size.width / 2) // or use (faqWebView.frame.size.width / 2)
    let yAxis = self.view.center.y // or use (view.frame.size.height / 2) // or use (faqWebView.frame.size.height / 2)



let frame = CGRect(x: (xAxis - 50), y: (yAxis - 50), width: 45, height: 45)
 activityIndicator = NVActivityIndicatorView(frame: frame)
activityIndicator.type = . ballScale // add your type
activityIndicator.color = UIColor.red // add your color

self.view.addSubview(activityIndicator) // or use  webView.addSubview(activityIndicator)
activityIndicator.startAnimating()
}

for hide

func webView(_ webView: UIWebView, didFailLoadWithError error: Error)
    {
        hideactivityIndicator()
    }

   func webViewDidFinishLoad(webView: UIWebView!)
    {
      hideactivityIndicator()
    }

  func hideactivityIndicator()
  {

  activityIndicator. stopAnimating()
  activityIndicator.removeFromSuperview()
  }
like image 135
Anbu.Karthik Avatar answered Nov 18 '22 07:11

Anbu.Karthik