如何实现单例微调器(activityindicator)?

时间:2019-07-01 05:09:28

标签: swift spinner uiactivityindicatorview

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

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

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

该如何使微调框为单例?

谢谢

2 个答案:

答案 0 :(得分:0)

我建议您创建一个util类并使该类成为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()
    }
}

如何使用此类

class ViewController: UIViewController {

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

答案 1 :(得分:0)

使用您的微调器创建一个Base Controller,然后继承您的其他控制器 Base Controller,并随时显示微调器< / strong>。

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

希望这会对您有所帮助。