协议可重复使用的旋转器一个Liner ProgressView

发布于 2025-02-09 11:33:43 字数 2589 浏览 1 评论 0原文

这是我目前的代码,而不是使用progressView作为

self.progressview = showprogressview()

我想将其用作self.self.ser.showprogress()作为一个衬里,

这是我当前的协议


protocol Loadable {
    var progressView: ProgressView? { get }
    func showProgressView() -> ProgressView
    func hideProgressView()
}

extension Loadable where Self: UIViewController {
    func showProgressView() -> ProgressView {
        let pv = ProgressView(frame: CGRect.zero)
        view.addSubview(pv)
        pv.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate(pv.setConstraints(boundsOf: view))
        UIView.animate(withDuration: 0.3) {
            pv.alpha = 1.0
        }
        return pv
    }

    func hideProgressView() {
        guard let progressView = progressView else { return }

        UIView.animate(withDuration: 0.6) {
            progressView.alpha = 0.0
        } completion: { finished in
            progressView.removeFromSuperview()
        }
    }
}```


//this is my current usage on the code


@objc private func loadData() {
        print("Calling API")
        
        self.progressView = showProgressView()
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 3.0){
            self.civilizationData()
            self.hideProgressView()
        }
    }

//tried using this method

protocol Loadable {
    var progressView: ProgressView? { get }
    func showProgressView()
    func hideProgressView()
}

extension Loadable where Self: UIViewController {
    func showProgressView()  {
        let progressView = ProgressView(frame: CGRect.zero)
        view.addSubview(progressView)
        progressView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate(progressView.setConstraints(boundsOf: view))
        UIView.animate(withDuration: 0.3) {
            progressView.alpha = 1.0
        }
        
    }

//but the added subview of my progressview doesn't disappear
protocol Loadable {
    var progressView: ProgressView? { get }
    func showProgressView()
    func hideProgressView()
}

extension Loadable where Self: UIViewController {
    func showProgressView()  {
        let progressView = ProgressView(frame: CGRect.zero)
        view.addSubview(progressView)
        progressView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate(progressView.setConstraints(boundsOf: view))
        UIView.animate(withDuration: 0.3) {
            progressView.alpha = 1.0
        }
        
    }

This is my code at the moment I instead of using progressView as

self.progressView = showProgressView()

I want to use it as self.showProgress() as a one liner

this is my current protocol


protocol Loadable {
    var progressView: ProgressView? { get }
    func showProgressView() -> ProgressView
    func hideProgressView()
}

extension Loadable where Self: UIViewController {
    func showProgressView() -> ProgressView {
        let pv = ProgressView(frame: CGRect.zero)
        view.addSubview(pv)
        pv.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate(pv.setConstraints(boundsOf: view))
        UIView.animate(withDuration: 0.3) {
            pv.alpha = 1.0
        }
        return pv
    }

    func hideProgressView() {
        guard let progressView = progressView else { return }

        UIView.animate(withDuration: 0.6) {
            progressView.alpha = 0.0
        } completion: { finished in
            progressView.removeFromSuperview()
        }
    }
}```


//this is my current usage on the code


@objc private func loadData() {
        print("Calling API")
        
        self.progressView = showProgressView()
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 3.0){
            self.civilizationData()
            self.hideProgressView()
        }
    }

//tried using this method

protocol Loadable {
    var progressView: ProgressView? { get }
    func showProgressView()
    func hideProgressView()
}

extension Loadable where Self: UIViewController {
    func showProgressView()  {
        let progressView = ProgressView(frame: CGRect.zero)
        view.addSubview(progressView)
        progressView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate(progressView.setConstraints(boundsOf: view))
        UIView.animate(withDuration: 0.3) {
            progressView.alpha = 1.0
        }
        
    }

//but the added subview of my progressview doesn't disappear
protocol Loadable {
    var progressView: ProgressView? { get }
    func showProgressView()
    func hideProgressView()
}

extension Loadable where Self: UIViewController {
    func showProgressView()  {
        let progressView = ProgressView(frame: CGRect.zero)
        view.addSubview(progressView)
        progressView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate(progressView.setConstraints(boundsOf: view))
        UIView.animate(withDuration: 0.3) {
            progressView.alpha = 1.0
        }
        
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文