Skip to content

Instantly share code, notes, and snippets.

@kyungpyoda
Created January 3, 2022 05:14
Show Gist options
  • Save kyungpyoda/f1f39f23456b6c710ab22d7c1627f1f5 to your computer and use it in GitHub Desktop.
Save kyungpyoda/f1f39f23456b6c710ab22d7c1627f1f5 to your computer and use it in GitHub Desktop.
[iOS] Global Loading Indicator
//
// LoadingIndicator.swift
//
// Created by 홍경표 on 2021/12/28.
//
import UIKit
public class LoadingIndicator {
private static weak var currentIndicator: UIActivityIndicatorView?
public class func showLoading() {
// keyWindow 객체 획득
guard let window = UIApplication.shared.windows.filter({ $0.isKeyWindow }).first else { return }
let loadingIndicatorView: UIActivityIndicatorView
if let existedView = window.subviews.first(where: { $0 is UIActivityIndicatorView }) as? UIActivityIndicatorView {
loadingIndicatorView = existedView
} else {
loadingIndicatorView = UIActivityIndicatorView(style: .large)
}
loadingIndicatorView.frame = window.frame
loadingIndicatorView.color = .white
loadingIndicatorView.backgroundColor = .black.withAlphaComponent(0.5)
window.addSubview(loadingIndicatorView)
loadingIndicatorView.startAnimating()
currentIndicator = loadingIndicatorView
}
public class func hideLoading() {
DispatchQueue.main.async {
UIView.animate(
withDuration: 0.3,
delay: 0,
options: .curveEaseInOut,
animations: {
currentIndicator?.alpha = 0
},
completion: { _ in
currentIndicator?.removeFromSuperview()
}
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment