Gif 움직이는 이미지는 따로 구해서 적용하시면 됩니다.
웹뷰 및 API 로딩 등 UI 적용
import UIKit
extension UIImageView {
static func fromGif(frame: CGRect, resourceName: String) -> UIImageView? {
guard let path = Bundle.main.path(forResource: resourceName, ofType: "gif") else {
print("Gif does not exist at that path")
return nil
}
let url = URL(fileURLWithPath: path)
guard let gifData = try? Data(contentsOf: url),
let source = CGImageSourceCreateWithData(gifData as CFData, nil) else { return nil }
var images = [UIImage]()
let imageCount = CGImageSourceGetCount(source)
for i in 0 ..< imageCount {
if let image = CGImageSourceCreateImageAtIndex(source, i, nil) {
images.append(UIImage(cgImage: image))
}
}
let gifImageView = UIImageView(frame: frame)
gifImageView.animationImages = images
return gifImageView
}
}
class LoadingHUD {
private static let sharedInstance = LoadingHUD()
private var backgroundView: UIView?
private var popupView: UIImageView?
private var loadingLabel: UILabel?
private var isRun: Bool = false
class func show() {
let backgroundView = UIView(frame: CGRect.init(x: 0, y: 0, width: 100, height: 100))
guard let popupView = UIImageView.fromGif(frame: CGRect.init(x: 0, y: 0, width: 100, height: 100), resourceName: "ico_loading_dot") else {
return
}
let loadingLabel = UILabel(frame: CGRect.init(x: 0, y: 0, width: 100, height: 100))
loadingLabel.text = "Loading ..."
loadingLabel.font = UIFont.boldSystemFont(ofSize: 15)
loadingLabel.textColor = .white
loadingLabel.textAlignment = .center
if let window = UIApplication.shared.keyWindow {
window.addSubview(backgroundView)
window.addSubview(popupView)
window.addSubview(loadingLabel)
// window.bringSubviewToFront(backgroundView)
// window.bringSubviewToFront(popupView)
backgroundView.frame = CGRect(x: 0, y: 0, width: window.frame.maxX, height: window.frame.maxY)
backgroundView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.4)
popupView.center = window.center
popupView.startAnimating()
loadingLabel.layer.position = CGPoint(x: window.frame.midX, y: popupView.frame.maxY - 10)
sharedInstance.backgroundView?.removeFromSuperview()
sharedInstance.popupView?.removeFromSuperview()
sharedInstance.loadingLabel?.removeFromSuperview()
sharedInstance.backgroundView = backgroundView
sharedInstance.popupView = popupView
sharedInstance.loadingLabel = loadingLabel
sharedInstance.isRun = true
}
}
class func hide() {
if let popupView = sharedInstance.popupView,
let loadingLabel = sharedInstance.loadingLabel,
let backgroundView = sharedInstance.backgroundView {
popupView.stopAnimating()
backgroundView.removeFromSuperview()
popupView.removeFromSuperview()
loadingLabel.removeFromSuperview()
}
sharedInstance.isRun = false
}
class func isLoading() -> Bool {
return sharedInstance.isRun
}
}
[사용방법]
LoadingHUD.show() => 노출
LoadingHUD.hide() => 숨김
'Swift > 기타' 카테고리의 다른 글
[IOS]App 네트워크 패킷 캡쳐 (0) | 2023.09.06 |
---|---|
LLDB 메모리 덤프 저장 (0) | 2023.09.06 |
[SWIFT]다른모양으로 반반 나눠서 선 그리기(애니메이션 효과 포함) (0) | 2023.07.19 |
[SWIFT]URLSession Post, Download, Upload / requestDecodable (0) | 2023.07.10 |
[SWIFT]Alamofire HTTPS TLS 에러 예외처리 (0) | 2023.07.04 |