Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create custom class Loading view using Swift

Tags:

ios

swift

loading

I am implementing loading concept by custom class loading view. I need to use this custom class by using simple self.show() and self.dismiss function. Here, I achieved this concept by using UIActivityController within UIAlertView but I need to do this using custom class.

Below code what I achieved by using UIActivityController within UIAlertView

func loadinHubShow() {
                let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)
                let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
                loadingIndicator.hidesWhenStopped = true
                loadingIndicator.style = UIActivityIndicatorView.Style.gray
                loadingIndicator.startAnimating();
                alert.view.addSubview(loadingIndicator)
                present(alert, animated: true, completion: nil)
    }

func loadinHubDismiss() {
     dismiss(animated: false, completion: nil)
}

How to change this to custom class loading view. I don’t want to use UIActivityController within UIAlertView, Alternatively need to create same look and feel UI design by UIView.

like image 956
Test App Avatar asked Sep 18 '25 15:09

Test App


2 Answers

import Foundation
import UIKit

public class Indicator {

    public static let sharedInstance = Indicator()
    var blurImg = UIImageView()
    var indicator = UIActivityIndicatorView()

    private init()
    {
        blurImg.frame = UIScreen.main.bounds
        blurImg.backgroundColor = UIColor.black
        blurImg.isUserInteractionEnabled = true
        blurImg.alpha = 0.5
        indicator.style = .whiteLarge
        indicator.center = blurImg.center
        indicator.startAnimating()
        indicator.color = .red
    }

    func showIndicator(){
        DispatchQueue.main.async( execute: {

            UIApplication.shared.keyWindow?.addSubview(self.blurImg)
            UIApplication.shared.keyWindow?.addSubview(self.indicator)
        })
    }
    func hideIndicator(){

        DispatchQueue.main.async( execute:
            {
                self.blurImg.removeFromSuperview()
                self.indicator.removeFromSuperview()
        })
    }
}
like image 87
Anil Kumar Avatar answered Sep 21 '25 04:09

Anil Kumar


import UIKit

class Loader: UIView {

var view: UIView!
@IBOutlet fileprivate weak var blurView: UIVisualEffectView!
@IBOutlet fileprivate weak var loader: UIActivityIndicatorView!

var targetView: UIView?

required init(forView view: UIView) {
    super.init(frame: view.bounds)
    targetView = view
    self._setup()
    targetView?.addSubview(self)
}
override init(frame: CGRect) {
    super.init(frame: frame)
    _setup()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self._setup()
}

private func _setup() {
    view = _loadViewFromNib()
    view.frame = bounds
    view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    view.translatesAutoresizingMaskIntoConstraints = true

    addSubview(view)
}

private func _loadViewFromNib() -> UIView {
    let bundle = Bundle(for: type(of: self))
    let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
    let nibView = nib.instantiate(withOwner: self, options: nil).first as! UIView

    return nibView
}

func showLoading(withCompletion completion: (() -> Swift.Void)? = nil) {
    UIView.animate(withDuration: 0.5, animations: { 
    }) { _ in
        completion?()
    }
}

func hideLoading() {
    UIView.animate(withDuration: 0.5, animations: {
    }) { _ in
        self.removeFromSuperview()
    }
}
}

You can call below functions in your ViewController

func showLoading() {
    if self.view.subviews.map({ $0 is Loader }).contains(true) {
        return
    }
    let loader = Loader(forView: self.view)
    loader.showLoading()
}

func hideLoading() {
    for view in self.view.subviews {
        if let view = view as? Loader {
            view.hideLoading()
            break
        }
    }
}
like image 37
Pal Singh Anand Avatar answered Sep 21 '25 06:09

Pal Singh Anand