0

I`ve implemented a UIView that display a CustomLottie in the center of the screen, it has show() and hide() methods.

How can I give it the ability of hide() it from another place than where show() was called?? This is the code:

class LottieProgressHUD: UIView {

    static let shared = LottieProgressHUD()
    let hudView: UIView
    var animationView: AnimationView

    //options
    var hudWidth:CGFloat    = 200
    var hudHeight:CGFloat   = 200
    var animationFileName   = "coinLoading"

    override init(frame: CGRect) {
        self.hudView = UIView()
        self.animationView = AnimationView()
        super.init(frame: frame)
        self.setup()
    }

    required init?(coder aDecoder: NSCoder) {
        self.hudView = UIView()
        self.animationView = AnimationView()
        super.init(coder: aDecoder)
        self.setup()
    }

    func setup() {
        self.addSubview(hudView)
        show()
    }

    override func didMoveToSuperview() {
        super.didMoveToSuperview()
        if let superview = self.superview {
            self.animationView.removeFromSuperview()


            let screenRect = UIScreen.main.bounds
            let screenWidth = screenRect.size.width
            let screenHeight = screenRect.size.height


            let width: CGFloat  = self.hudWidth
            let height: CGFloat = self.hudHeight
            self.frame = CGRect(x: (screenWidth / 2) - (width / 2) ,y: (screenHeight / 2) - (height / 2), width: width, height: height)
            hudView.frame = self.bounds
            layer.masksToBounds = true

            self.animationView = AnimationView(name: self.animationFileName)
            self.animationView.frame = CGRect(x: 0, y: 0, width: self.hudView.frame.width, height: self.hudView.frame.size.height)
            self.animationView.contentMode = .scaleAspectFill

            self.hudView.addSubview(animationView)
            self.animationView.loopMode = .loop
            self.animationView.play()

            self.hide()
        }
    }

    func show() {
        self.isHidden = false
    }

    func hide() {
        self.isHidden = true
    }
}

This is how I call it inside the VC:

let progressHUD = LottieProgressHUD.shared 
self.view.addSubview(progressHUD)
progressHUD.show()

I would like to call progressHUD.hide() inside the VM

1 Answers1

0

You already have a static property that you can reference the same instance from anywhere. Should be able to call your show and hide methods from any class.

LottieProgressHUD.shared.show()

LottieProgressHUD.shared.hide()
clawesome
  • 1,223
  • 1
  • 5
  • 10