如何实现单例微调器(活动指示器)?

how can I implement a singleton spinner (activityindicator)?

我想实现一个单例微调器,因为任何具有 api 的应用程序都需要一个微调器。好吧,我可以编写微调器自定义代码,但我的问题是,如果我想显示它,我应该编写下一行代码。

    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()

这不太好,因为每次我想显示微调器时我都必须放这行,好吧,我的微调器 class 是下一个。我正在使用 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")
}
}

我应该怎么做才能使微调器成为单例?

谢谢

我建议您创建一个 util class 并使其成为 class 单例。

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()
    }
}

如何使用这个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
    }
}

你的微调器创建一个Base Controller,然后继承你的其他控制器Base Controller随时显示微调器

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.
    }
}

希望对您有所帮助。