Skip to content

Instantly share code, notes, and snippets.

@likil33
Created July 2, 2024 18:28
Show Gist options
  • Select an option

  • Save likil33/7598c1d875abec66bfb9198406094bfc to your computer and use it in GitHub Desktop.

Select an option

Save likil33/7598c1d875abec66bfb9198406094bfc to your computer and use it in GitHub Desktop.
Loading indicator - default and. third party lib. NVActivityIndicatorView
//Default
import Foundation
import UIKit
class LoaingView:UIView {
public static let shared = LoaingView()
private var indicator:UIActivityIndicatorView = {
let indicator = UIActivityIndicatorView()
indicator.color = .black
indicator.backgroundColor = .gray.withAlphaComponent(0.5)
indicator.style = .large
indicator.translatesAutoresizingMaskIntoConstraints = false
return indicator
}()
func startAnimating(_ loadView:UIView) {
self.indicator.startAnimating()
loadView.addSubview(self.indicator)
indicator.topAnchor.constraint(equalTo: loadView.topAnchor,constant: 0).isActive = true
indicator.bottomAnchor.constraint(equalTo: loadView.bottomAnchor,constant: 0).isActive = true
indicator.leftAnchor.constraint(equalTo: loadView.leftAnchor,constant: 0).isActive = true
indicator.rightAnchor.constraint(equalTo: loadView.rightAnchor,constant: 0).isActive = true
indicator.bringSubviewToFront(loadView)
}
func stopAnimating() {
indicator.stopAnimating()
indicator.removeFromSuperview()
}
}
////////////NVActivityIndicatorView
pod 'NVActivityIndicatorView'
import Foundation
import UIKit
import NVActivityIndicatorView
class LoaingView:UIView {
public static let shared = LoaingView()
var backgroundView = UIView()
private var indicator:NVActivityIndicatorView = {
let indicator = NVActivityIndicatorView(frame: CGRect.init(x: 0, y: 0, width: 50, height: 50),type: .ballRotateChase, color: .white,padding: 50)
// indicator.backgroundColor = .gray.withAlphaComponent(0.5)
indicator.translatesAutoresizingMaskIntoConstraints = false
return indicator
}()
func startAnimating(_ loadView:UIView) {
self.indicator.startAnimating()
loadView.addSubview(self.backgroundView)
self.backgroundView.frame = loadView.frame
backgroundView.backgroundColor = .gray.withAlphaComponent(0.5)
backgroundView.addSubview(self.indicator)
indicator.centerYAnchor.constraint(equalToSystemSpacingBelow: self.backgroundView.centerYAnchor, multiplier: 0).isActive = true
indicator.centerXAnchor.constraint(equalToSystemSpacingAfter: self.backgroundView.centerXAnchor, multiplier: 0).isActive = true
indicator.widthAnchor.constraint(equalToConstant: 50).isActive = true
indicator.heightAnchor.constraint(equalToConstant: 50).isActive = true
backgroundView.bringSubviewToFront(loadView)
}
func stopAnimating() {
DispatchQueue.main.async {
self.indicator.stopAnimating()
self.indicator.removeFromSuperview()
self.backgroundView.removeFromSuperview()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment