Search code examples
swiftspinneruiactivityindicatorview

how can I implement a singleton spinner (activityindicator)?


i want to implement a singleton spinner, because any app that has a api needs a spinner. well I can code a spinner custom, but my problem is that I should code the next line ever if I want to show it.

    let rosetaGIF = UIImage(named: "wheel.png")
    let ind = MyIndicator(frame: CGRect(x: 0, y: 0, width: (rosetaGIF?.size.width)!/2, height: (rosetaGIF?.size.height)!/2), image: rosetaGIF!)
    view.addSubview(ind)
    view.alpha = 0.5
    ind.startAnimating()

that's not good, because I must put this lines every time that I want to show the spinner, well, my spinner class is the next. I'm using swift 4.2

import UIKit

class MyIndicator: UIActivityIndicatorView {


let loadingView = UIView(frame: (UIApplication.shared.delegate?.window??.bounds)!)
let imageView = UIImageView()
let sizeView = UIViewController()

init(frame: CGRect, image: UIImage) {
    super.init(frame: frame)

    imageView.frame = bounds
    imageView.image = image
    imageView.contentMode = .scaleAspectFit
    imageView.center = CGPoint(x: sizeView.view.frame.width/2, y: sizeView.view.frame.height/2)
    imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    addSubview(imageView)
}

required init(coder: NSCoder) {
    fatalError()
}

override func startAnimating()
{
    isHidden = false
    rotate()
}

override func stopAnimating()
{
    isHidden = true
    removeRotation()
}

private func rotate() {
    let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
    rotation.toValue = NSNumber(value: Double.pi * 1)
    rotation.duration = 1
    rotation.isCumulative = true
    rotation.repeatCount = Float.greatestFiniteMagnitude
    self.imageView.layer.add(rotation, forKey: "rotationAnimation")
}

private func removeRotation() {
    self.imageView.layer.removeAnimation(forKey: "rotationAnimation")
}
}

what should I do for that the spinner will be singleton?

thanks


Solution

  • I suggest you create a util class and make that class Singleton.

    import UIKit
    class Util {
        static let shared = Util()
        private init(){}
    
        var loader: MyIndicator?
    
        func showLoader(view: UIView){
            hideLoader()
            let rosetaGIF = UIImage(named: "wheel.png")
             loader = MyIndicator(frame: CGRect(x: 0, y: 0, width: (rosetaGIF?.size.width)!/2, height: (rosetaGIF?.size.height)!/2), image: rosetaGIF!)
            view.addSubview(loader!)
            view.alpha = 0.5
            loader?.startAnimating()
        }
    
        func hideLoader(){
            loader?.stopAnimating()
            loader?.removeFromSuperview()
        }
    }
    

    How to use this class

    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            Util.shared.showLoader(view: view) // for showing the loader
            Util.shared.hideLoader() // for hiding the loader
        }
    }