首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模仿SVProgressHUD行为

模仿SVProgressHUD行为
EN

Stack Overflow用户
提问于 2020-02-14 21:54:58
回答 1查看 72关注 0票数 0

我已经实现了一个在屏幕中央显示CustomLottie的UIView,它有CustomLottie()和hide()方法。

我如何才能让它从调用show()的地方以外的其他地方隐藏()它呢?代码如下:

代码语言:javascript
复制
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
    }
}

这是我在VC内部调用它的方式:

代码语言:javascript
复制
let progressHUD = LottieProgressHUD.shared 
self.view.addSubview(progressHUD)
progressHUD.show()

我想在VM中调用progressHUD.hide()

EN

回答 1

Stack Overflow用户

发布于 2020-02-14 22:09:59

您已经有了一个静态属性,可以从任何地方引用相同的实例。应该能够从任何类调用您的显示和隐藏方法。

代码语言:javascript
复制
LottieProgressHUD.shared.show()

LottieProgressHUD.shared.hide()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60227716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档