(Swift) Loading loading circle view

created at 11-14-2021 views: 12

1) Usage scenario

  • Mostly used for network requests to load and rotate;
  • Support rotation style adjustment;
  • Support circle + text display;
  • If the circle view is already displayed, remove it before adding it to avoid layer accumulation;
    The effect is as follows;

Default effect:

Default effect

With text display

loading

Instructions

⚠️⚠️⚠️Note that some methods need to be called in the main thread;
Extend to UIView, applicable to any objects inherited from UIView and its subviews;
Only one line of code can be rendered;

// basic usage
view.showLoading()
// with a text description
view.showLoading("Loading")
// immediately hide "loadingView" in self.view
view.hideLoading()

Source code sharing

//
//  CCLoadingView.swift
//  HelloSwift
//
//  Created by a51095 on 2021/7/16.
//

final class CCLoadingView: UIView {
    ///Lazy loading, prompt label
    private lazy var messageLabel: UILabel = {
        let l = UILabel()
        l.textColor = .white
        l.font = RegularFont(16)
        l.textAlignment = .center
       return l
    }()

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK:-Deinitializer
    deinit { print("CCLoadingView deinit~") }

    // MARK:-Initializer
    init(toast: String) {
        super.init(frame: .zero)

        let contentView = UIView()
        contentView.layer.cornerRadius = 5
        contentView.backgroundColor = .hexColor("#000000", 0.5)
        addSubview(contentView)

        let activity = UIActivityIndicatorView(style: .white)
        contentView.addSubview(activity)

        if !toast.isEmpty {
            // Middle content view
            contentView.snp.makeConstraints { (make) in
                make.center.equalToSuperview()
                make.size.equalTo(CGSize(width: 120, height: 100))
            }

            // Load circle view
            activity.snp.makeConstraints { (make) in
                make.top.equalTo(16)
                make.centerX.equalToSuperview()
                make.size.equalTo(CGSize(width: 37, height: 37))
            }

            // Text prompt view
            messageLabel.text = toast
            addSubview(messageLabel)
            messageLabel.snp.makeConstraints { (make) in
                make.centerX.equalToSuperview()
                make.bottom.equalTo(contentView.snp.bottom)
                make.size.equalTo(CGSize(width: 120, height: 36))
            }
        }else {
            // Middle content view
            contentView.snp.makeConstraints { (make) in
                make.center.equalToSuperview()
                make.size.equalTo(CGSize(width: 100, height: 100))
            }

            // Load circle view
            activity.snp.makeConstraints { (make) in
                make.center.equalToSuperview()
            }
        }
        activity.startAnimating()
    }
}

extension UIView {
    /// Display the loading box (refresh the UI in the main thread)
    func showLoading(_ message: String = "") {
        // If CCLoadingView has been loaded in the current view, remove it before adding it;
        if let lastView = subviews.last as? CCLoadingView { lastView.removeFromSuperview() }

        let loadingView = CCLoadingView(toast: message)
        addSubview(loadingView)
        loadingView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }

    /// Hide the loading box (refresh the UI in the main thread)
    func hideLoading() {
        for item in subviews {
            if item.isKind(of: CCLoadingView.self) {
                item.removeFromSuperview()
            }
        }
    }
}
created at:11-14-2021
edited at: 11-14-2021: