3
\$\begingroup\$

I have a login screen on my iOS app. When I click login button I want to make sure that user should be displayed a loading image in center of the screen.

I found lots of different methods online but I am after a simplest method. I found a way to display activity Indicator with minimum code:

First of all, import your UI Kit:

import UIKit;

Then you have to initiate activity indicator:

let activityIndicator:UIActivityIndicatorView = UIActivityIndicatorView();

Then just copy paste this functions below which are self explainatory and call them whereever you need them:

func startLoading(){
    activityIndicator.center = self.view.center;
    activityIndicator.hidesWhenStopped = true;
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray;
    view.addSubview(activityIndicator);

    activityIndicator.startAnimating();
    UIApplication.shared.beginIgnoringInteractionEvents();

}

func stopLoading(){ 

    activityIndicator.stopAnimating();
    UIApplication.shared.endIgnoringInteractionEvents();

}

Although, I am still not convinced that it's the best way to do it. Could it still be reduced?

Here is how my screen looks with the code:

enter image description here

\$\endgroup\$
  • \$\begingroup\$ are you intending to use this activity indicator on multiple screens, or just on one viewController? \$\endgroup\$ – dirtydanee Dec 19 '16 at 21:48
  • \$\begingroup\$ multiple screen because I want to use it on register, login even inside my protected controller section \$\endgroup\$ – harshal lonare Dec 19 '16 at 23:39
1
\$\begingroup\$

There are a few things I would recommend to do.

  1. Usually, instead of disabling the interaction events there is an extra layer on top of all views that contains the activity indicator. So, instead of adding the activity indicator directly into the viewController's view, add it in another view first with clear background color and with 0.4 alpha.

  2. Right now the startLoading() function also sets up the activity indicator, what i would rather separate into a different function, like setupLoading()

  3. If you want to reuse the solution on other viewControllers, create a class or a protocol at least ecapsulating the logic. Also, it is better to present it on the UIApplication's keyWindow property, than just simply adding it to viewContollers view's.

  4. Prepare for the stopLoading() and startLoading() functions to be called from a background thread.

Finally, there are plenty of very good solutions already on the interwebs, that you could integrate into your project, like SwiftSpinner.

If you need further help, code examples, let me know and I will edit my answer.

\$\endgroup\$
0
\$\begingroup\$

1 It's better to have separate singleton class which will take care of all the activity indicator related tasks.
So it'll be easy to reuse it from anywhere the app.

2 Make variables and methods private if not required outside.

3 Also make sure that whenever this class perform anything related to UI, it must be on main thread.

4 Add activity indicator on root view controller, so no restrictions that it should be called from any view.

class LoaderController: NSObject {

static let sharedInstance = LoaderController()
private let activityIndicator = UIActivityIndicatorView()

//MARK: - Private Methods -
private func setupLoader() {
    removeLoader()

    activityIndicator.hidesWhenStopped = true
    activityIndicator.activityIndicatorViewStyle = .gray
}

//MARK: - Public Methods -
func showLoader() {
    setupLoader()

    let appDel = UIApplication.shared.delegate as! AppDelegate
    let holdingView = appDel.window!.rootViewController!.view!

    DispatchQueue.main.async {
        self.activityIndicator.center = holdingView.center
        self.activityIndicator.startAnimating()
        holdingView.addSubview(self.activityIndicator)
        UIApplication.shared.beginIgnoringInteractionEvents()
    }
}

func removeLoader(){
    DispatchQueue.main.async {
        self.activityIndicator.stopAnimating()
        self.activityIndicator.removeFromSuperview()
        UIApplication.shared.endIgnoringInteractionEvents()
    }
}
}

Here's example use:

//call from any thread, any class
LoaderController.sharedInstance.showLoader()
LoaderController.sharedInstance.removeLoader()
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.