0

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

2
  • i think it will be helpful fo you youtube.com/watch?v=cMCKAr8bt84 youtube.com/watch?v=cMCKAr8bt84 Commented Jul 1, 2019 at 5:11
  • so I suggest you to use a BaseViewController, implement your indicator there and inherit your other ViewControllerS from the BaseViewController. then whenever you want the spinner, you can call it from your ChildViewController. Commented Jul 1, 2019 at 5:16

2 Answers 2

0

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
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Create a Base Controller with your spinner, then inherit other Controllers from you Base Controller and show the spinner whenever you want.

class BaseViewController: UIViewController {

    var activityIndicator: UIActivityIndicatorView!

    override func viewDidLoad() {
        super.viewDidLoad()

        activityIndicator = UIActivityIndicatorView(frame: CGRect(x: (UIScreen.main.bounds.width-40)/2, y: (UIScreen.main.bounds.height-40)/2, width: 40, height: 40))
        activityIndicator.transform = CGAffineTransform(scaleX: 2, y: 2)
        activityIndicator.color = .darkGray
        view.addSubview(activityIndicator)
    }
}

class ViewController: BaseViewController {


    override func viewDidLoad() {
        super.viewDidLoad()

        activityIndicator.startAnimating() // you can show the spinner wherever you want.
    }
}

hope this will help to you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.