Implement activity indicator in protocol mode

I want to create a protocol that will give me the ability to add / remove an activity indicator to / from the UIViewController and its subclasses when they comply with this protocol.

so I got code like this

protocol ActivityIndicatorPresenter {
    var activityIndicator: UIActivityIndicatorView { get }
    func showActivityIndicator()
    func hideActivityIndicator()
}

extension ActivityIndicatorPresenter where Self: UIViewController {
    func showActivityIndicator() {
        dispatch_async(dispatch_get_main_queue()) {
            self.activityIndicator.color = UIColor.blackColor()
            self.activityIndicator.frame = CGRect(x: 0.0, y: 0.0, width: 80.0, height: 80.0)
            self.activityIndicator.center = CGPoint(x:self.view.bounds.size.width / 2, y:self.view.bounds.size.height / 2)
            self.view.addSubview(self.activityIndicator)
            self.activityIndicator.startAnimating()
    }
}

    func hideActivityIndicator() {
        dispatch_async(dispatch_get_main_queue()) {
            self.activityIndicator.stopAnimating()
            self.activityIndicator.removeFromSuperview()
        }
    }
}

and in a subclass of UIViewController:

class MyViewController: ActivityIndicatorPresenter {
    var activityIndicator: UIActivityIndicator { get {return self.spinner}}
    var spinner = UIActivityIndicator()
}

Everything works, but I think that returning the stored property to the computed getter property is a workaround. Is it better to implement it?

+4
source share

All Articles