Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use activity indicator in many VC without duplicating code swift

Tags:

ios

swift

I have two ViewControllers (A and B) in swift IOS. Both A and B loads data from internet (separately). I want to display activityIndicator while loading. I know I can do it the bad way by declaring it once in each VC as follows

ViewController A

var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()

func activityIndicatorBegin() {
    activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0,0,50,50))
    activityIndicator.center = self.view.center
    activityIndicator.hidesWhenStopped = true
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
    view.addSubview(activityIndicator)
    activityIndicator.startAnimating()
    disableUserInteraction()

    greyView = UIView()
    greyView.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height)
    greyView.backgroundColor = UIColor.blackColor()
    greyView.alpha = 0.5
    self.view.addSubview(greyView)
}

func activityIndicatorEnd() {
    self.activityIndicator.stopAnimating()
    enableUserInteraction()
    self.greyView.removeFromSuperview()
}

and do the exact same for ViewController B and call the activityIndicatorBegin and activityIndicatorEnd functions that is declared in B view controller. However, I want to make the code cleaner. How could it be done? I am trying to make code cleaner these days.

Thanks,

--UPDATE--

I would imageing something like the following code would work. But it doesnt because I cant declare variables in extensions

    var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()

extension UIViewController {
    func activityIndicatorBegin() {
    activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0,0,50,50))
    activityIndicator.center = self.view.center
    activityIndicator.hidesWhenStopped = true
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
    view.addSubview(activityIndicator)
    activityIndicator.startAnimating()
    disableUserInteraction()

    greyView = UIView()
    greyView.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height)
    greyView.backgroundColor = UIColor.blackColor()
    greyView.alpha = 0.5
    self.view.addSubview(greyView)
}

func activityIndicatorEnd() {
    self.activityIndicator.stopAnimating()
    enableUserInteraction()
    self.greyView.removeFromSuperview()
    }
}
like image 652
user172902 Avatar asked Jul 19 '16 11:07

user172902


Video Answer


2 Answers

SWIFT 4

extension UIView{

    func activityStartAnimating(activityColor: UIColor, backgroundColor: UIColor) {
        let backgroundView = UIView()
        backgroundView.frame = CGRect.init(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height)
        backgroundView.backgroundColor = backgroundColor
        backgroundView.tag = 475647
        
        var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
        activityIndicator = UIActivityIndicatorView(frame: CGRect.init(x: 0, y: 0, width: 50, height: 50))
        activityIndicator.center = self.center
        activityIndicator.hidesWhenStopped = true
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
        activityIndicator.color = activityColor
        activityIndicator.startAnimating()
        self.isUserInteractionEnabled = false
        
        backgroundView.addSubview(activityIndicator)

        self.addSubview(backgroundView)
    }

    func activityStopAnimating() {
        if let background = viewWithTag(475647){
            background.removeFromSuperview()
        }
        self.isUserInteractionEnabled = true
    }
}

USAGE:

self.view.activityStartAnimating(activityColor: UIColor.white, backgroundColor: UIColor.black.withAlphaComponent(0.5))


self.view.activityStopAnimating()
like image 163
jeydiz Avatar answered Nov 10 '22 05:11

jeydiz


Create one BaseViewController and add these two method and declare activityIndicator object inside that BaseViewController. Now make BaseViewController as parent class of all your ViewController. After that if you want to add the indicator simply call like this

1) Create BaseViewController

class BaseViewController: UIViewController {
    var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func activityIndicatorBegin() {
        activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0,0,50,50))
        activityIndicator.center = self.view.center
        activityIndicator.hidesWhenStopped = true
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
        view.addSubview(activityIndicator)
        activityIndicator.startAnimating()
        disableUserInteraction()

        greyView = UIView()
        greyView.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height)
        greyView.backgroundColor = UIColor.blackColor()
        greyView.alpha = 0.5
        self.view.addSubview(greyView)
    }

    func activityIndicatorEnd() {
        self.activityIndicator.stopAnimating()
        enableUserInteraction()
        self.greyView.removeFromSuperview()
    }
}

2) Now assign this BaseViewController as parent of your all viewController like this

class ViewController1: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //Now if you want to add activityIndicator call like this
        self.activityIndicatorBegin()
        //or if you want remove activityIndicator 
        self.activityIndicatorEnd()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    } 
}
like image 27
Nirav D Avatar answered Nov 10 '22 05:11

Nirav D